Change color scheme when calling vimdiff inside Vim

4

1

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
else
    colorscheme molokai
endif

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?

Somebody still uses you MS-DOS

Posted 2010-06-28T12:55:37.880

Reputation: 4 143

Answers

0

garyjohn, thanks for your time, but I find my solution. It works!

au FilterWritePost * if &diff | set t_Co=256 | set bg=dark | colorscheme peaksea | else | colorscheme molokai | endif
au BufWinLeave * colorscheme molokai

Source

Somebody still uses you MS-DOS

Posted 2010-06-28T12:55:37.880

Reputation: 4 143

6

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.

garyjohn

Posted 2010-06-28T12:55:37.880

Reputation: 29 085

Note, that Vim 7.4 restores your settings when turning off diff mode. – Christian Brabandt – 2014-10-16T08:38:54.393

Interesting. Indeed, it works. Do you know a solution to "come back" to the original colorscheme when I end VimDiff with :q? – Somebody still uses you MS-DOS – 2010-06-28T17:07:30.733

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 – garyjohn – 2010-06-28T17:40:13.227

I moved the contents of the above comment to the answer. – garyjohn – 2010-06-28T17:57:14.763

1

Unfortunately Vim doesn't have explicit diff mode events. You can hack around that to some extent with the suggestions from the other answers.

As an alternative to that approach, you can create a function that wraps diffthis and diffoff and change your settings in that function.

nnoremap <leader>df :call ToggleDiff()<CR>

function! ToggleDiff()
    if &diff
        diffoff
        setlocal syntax=on
    else
        diffthis
        setlocal syntax=off
    endif
endfunction

Jonathan Potter

Posted 2010-06-28T12:55:37.880

Reputation: 271