Change color scheme when calling vimdiff inside Vim


I'm using the VCSCommand plugin.

I'm calling VimDiff utility inside Vim by calling :VCSVimDiff.

I have in my vimrc:

if &diff
    set t_Co=256
    set background=dark
    colorscheme peaksea
    colorscheme molokai

It works when I call vimdiff from my console, but not when I call it from Vim using VCS.

Is this a plugin problem, or a vimrc config that is missing?

Best Answer

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.