Vim reads your vimrc once, at startup. The if &diff
statement is executed when it is read, not every time the state of 'diff'
changes. One way to have those color commands executed when you execute :VCSVimDiff
is to put them in an autocommand in your vimrc, like this.
au FilterWritePre * if &diff | set t_Co=256 | set bg=dark | colorscheme peaksea | endif
where the FilterWritePre
event is one that is triggered when Vim performs a diff.
[The comment didn't work well, so I'll add to my original answer.]
If you want to end VimDiff with :q
, what you could do is set up another autocommand, maybe using the BufWinLeave
event, again testing &diff
and executing the commands to set your default colorscheme.
What I do is use the following command to delete the buffer I had diff'd against, turn off diff mode and restore some saved settings.
command! -bar -bang Nodiff wincmd l <bar> only<bang> <bar> set nodiff noscrollbind scrollopt-=hor wrap foldcolumn=0 virtualedit= foldlevel=99 <bar> if exists("b:fdm") <bar> let &fdm = b:fdm <bar> endif <bar> if exists("b:syn") <bar> let &syn = b:syn <bar> endif
To make and/or save those settings when diff mode is entered, I use the following autocommands.
au FilterWritePre * if &diff | set virtualedit=all | endif
au FilterWritePre * exe 'let b:syn = &syn | if &diff | set syn=OFF | endif'
au BufWinEnter * if &fdm != "diff" | let b:fdm = &fdm | endif
Those commands have evolved over the years, which is my excuse for their inconsistencies.
Below are some hacks I've developed. They are not elegant, but may be functional in your corporate environment.
HOMEDRIVE Only
It seems that many applications only use HOMEDRIVE / HOMEPATH. In that case, you can create a startup script that remaps the base drive letter to your local user path via the UNC drive admin path:
set HOME
HOMEDRIVE=G:
HOMEPATH=\
HOMESHARE=\\Server\Users\username
net use g: /delete
net use g: \\localhost\C$\Users\username
HOMEDRIVE Local Default
If you do not need to access "Server" by name at all, you can cause the group policy setting to fail and fall back to your local machine. The easiest way to do this is to add an entry to C:\Windows\System32\drivers\etc\hosts like:
127.0.0.1 Server
After rebooting, you should see something like:
set HOME
HOMEDRIVE=C:
HOMEPATH=\Users\username
HOMEDRIVE/SHARE with Hybrid Local/Remote UNC Paths
If you want access to "Server" by name for some UNC paths, but override others with local paths, I have developed the following abomination. Note: direct server connections to "Server" will still resolve to your local machine. I recommend this solution only if "Server" is only a file server:
Modify C:\Windows\System32\drivers\etc\hosts to redirect "Server" to your local machine:
127.0.0.1 Server
Add the following Multi-String registry value to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0 to allow credentials to be passed to the local UNC path:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa\MSV1_0\
BackConnectionHostNames = Server
Create a dummy directory that will serve as the root of Server:
set DUMMY_LOC=C:\Server_Dummy
mkdir %DUMMY_LOC%
cd /D %DUMMY_LOC%
For each UNC path you want to direct to the real Server:
rem Alternatively you can use an IP below, but it is more likely to break if DNS changes
set SERVER_FQDN=Server.network.blah.com
rem Take a look at what's available...
net view \\%SERVER_FQDN%\
mklink /D Remote_Example \\%SERVER_FQDN%\Remote_Example
net share Remote_Example=%DUMMY_LOC%\Remote_Example /grant:everyone,FULL
For each UNC share you want to define locally (such as Users):
rem The link isn't really necessary for the share, I just find it easier to manage when all of these hacks are in the same directory
mklink /D Users C:\Users
net share Users=%DUMMY_LOC%\Users /grant:everyone,FULL
Reboot
For the example, this would allow the following UNC paths to be resolved:
\\Server\Remote_Example => \\Server.network.blah.com\Remote_Example
\\Server\Users => C:\Users
This path resolution should occur prior to drive mappings. As long as the UNC paths associated with the mappings are valid (be they local or remote), drive letters should behave as expected.
For example, in my setup the following variables are forced by the domain:
set HOME
HOMEDRIVE=G:
HOMEPATH=\
HOMESHARE=\\Server\Users\username
But due to my mappings, the result is:
G: => \\Server\Users\username => C:\Users\username
Best Answer
This issue can be caused by the default _vimrc file created by the installer on Windows. If you're still using that default file, or if you copied it at some point, then check the function you've assigned to the
diffexpr
option. One of the patches between Vim 7.3 and 7.4 introduced new default quoting rules for the cmd.exe shell on Windows. This patch broke the workaround in the MyDiff() function designed to fix the same issue solved by the patch.The MyDiff() function was fixed by version 7.4.103 by fixing the installer. Here is the MyDiff() function which the latest installer will create for you if you just want to copy it to your _vimrc:
You can see your full version in Vim using the
:version
or:intro
commands, or at the splash screen at startup.Unfortunately if you want an official installer, you will either need to wait until 8.0, or install a nightly build. Nevertheless, you can install Vim from other places or build your own Vim.
Duplicated on Stack Overflow (unusually, on-topic on both sites), if this answer is updated so should the other.