178

How to load a different colorscheme when doing vimdiff.

I want this because the my current colorscheme does not show some diffs properly in vimdiff, For. eg some diff is shown with same fg/bg color. This makes it very hard to understand the diff. So every time i do a vimdiff i have to do :colorscheme some_other_scheme

Can this be done in .vimrc file?

CodeRain
  • 6,334
  • 4
  • 28
  • 33

14 Answers14

213

I don't know why vim uses so many colors to highlight with, it doesn't really help you figure out what's going on.

I modified my colorscheme to only use one color to highlight (with another to show where theres a difference within a line) and it made all the difference.

Before

enter image description here

After

colorscheme_screenshot

I did this by adding the following to the end of my colorscheme file (~/.vim/colors/mycolorscheme.vim).

highlight DiffAdd    cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffDelete cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffChange cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
highlight DiffText   cterm=bold ctermfg=10 ctermbg=88 gui=none guifg=bg guibg=Red
  • cterm - sets the style
  • ctermfg - set the text color
  • ctermbg - set the highlighting
  • DiffAdd - line was added
  • DiffDelete - line was removed
  • DiffChange - part of the line was changed (highlights the whole line)
  • DiffText - the exact part of the line that changed

I used this link as a reference for the color numbers.

Note: I didn't set the gui options because I use a different colorscheme for macvim/gvim

Sergey Antopolskiy
  • 3,970
  • 2
  • 24
  • 40
Dean
  • 8,632
  • 6
  • 45
  • 61
  • 3
    Thanks! This looks much better! – vitaluha Mar 20 '14 at 13:18
  • 4
    Like pabo said in another comment, you may need to name your color scheme file like mycolorscheme.vim, with .vim extension on the end. – David Winiecki Mar 14 '15 at 00:12
  • Thanks! Minor nit: I think you accidentally made all "guibg"s red. Did you intend to have two colors -- one for DiffText and one for the rest? – Kapil Arya Jan 08 '16 at 05:54
  • @KapilArya Because I have a gui-specific colorsheme, I have no idea what those settings are doing :P Please edit if you think there's something more appropriate. – Dean Jan 08 '16 at 21:55
  • 7
    Why when using numbers (ctermbg=17) colors don't work, but when using words ( ctermbg=blue ) - it works ? .. solution: terminal only has 8 colors 1-7 – Ricky Levi Feb 22 '16 at 20:51
  • 5
    MacOS using iTerm2. No matter what I did, I got: Cannot find colour scheme '~/.vim/colors/mycolorscheme' Copying the color codes directly inbetween the "if &diff" statement in ~/.vimrc did the trick. – Tony Barganski Sep 19 '18 at 15:03
  • @Dean what if you want to pass ( no special highlighting leave it as normal mode ) some style then what will you enter as a value ? – Tripurari Shankar Dec 19 '18 at 08:10
  • @TripurariShankar I'm not sure – Dean Dec 19 '18 at 18:22
  • 7 years on, I'm here to say that http://vim.wikia.com/wiki/Xterm256_color_names_for_console_Vim?file=Xterm-color-table.png still works and the `color_demo.vim` script really helps. Just be sure to open it in a separate buffer :P – icedwater Jun 16 '20 at 09:26
  • considering the vim in your screenshots have a dark background i found [Dimtry's answer](https://stackoverflow.com/a/24098619/4955266) to fix the real issue with vim's diff colorsheme; the real culprit is vim doesn't know its being used in a terminal with a dark background – Fuseteam Dec 16 '20 at 13:48
  • This is definitely an improvement. Since I'm on a dark green background I might need to tweak it but this is very aesthetically pleasing. Thanks for the screenshots. – Sridhar Sarnobat Apr 29 '21 at 14:51
147

If you're calling vimdiff from the command-line, put the following in your .vimrc:

if &diff
    colorscheme some_other_scheme
endif

(if you're already in vimdiff => :colorscheme some_other_scheme thanks dlaehnemann)


If you're using vimdiff from within vim, you'd either have to override the commands you use to start/stop it (e.g. diffthis, diffoff) using :cnoreabbr (there's also a plugin) or use an autocommand:

au FilterWritePre * if &diff | colorscheme xyz | endif

FilterWritePre is called before filtering through an external program (the diff utility) and the &diff-option is set by vim when it's going into diff-mode (among others, see :help diff)

I'm not sure which autocommand to use to return to the original colorscheme though.

4wk_
  • 2,458
  • 3
  • 34
  • 46
DataWraith
  • 3,274
  • 1
  • 19
  • 17
  • Is it possible to call more than one command between the two pipes "|"? I would be interested in return to original colorscheme after using vimdiff from vim too... – Somebody still uses you MS-DOS Jun 14 '10 at 13:30
  • 2
    The pipes are substitues for newlines, similar to `;` in shell scripts, so you can just add new 'lines': `if &diff | colorscheme xyz | cmd2 | cmd3 | endif` – DataWraith Jun 15 '10 at 14:17
  • 2
    `colorscheme some_other_scheme` will look through your runtime paths for `color/some_other_scheme.vim`. You can see your runtimepaths by `:set runtimepath?` from within vim. – pabo Dec 12 '14 at 18:56
  • 3
    For console users (like myself), I found 'vividchalk' to be the best colorscheme (out of the default install set) for maintaining contrast between highlighted diffs and highlighted syntax, at least for shell scripts. A close second was 'evening.' Hope that helps. – Conrad Meyer Oct 09 '18 at 21:04
  • My problem is always the lack of contrast with the syntax highlighting, which loses whole chunks of text in diff coloring. I used ':syntax off' as needed in the past, but just added it to my .vimrc in the 'if &diff' predicate and now saved some keystrokes – qneill Jan 24 '19 at 17:07
  • I recommend 'blue' for vimdiff. It is installed by default and works well. Works for (some) colorblind people to. – sqqqrly Apr 10 '23 at 16:24
83

To answer my own question:

if &diff
    colorscheme evening
endif
CodeRain
  • 6,334
  • 4
  • 28
  • 33
28

molokai: molokai color scheme github: github color scheme The two themes github and molokai are equally beautiful.

curl -fLo ~/.vim/colors/molokai.vim --create-dirs https://raw.githubusercontent.com/tomasr/molokai/master/colors/molokai.vim
curl -fLo ~/.vim/colors/github.vim --create-dirs https://raw.githubusercontent.com/endel/vim-github-colorscheme/master/colors/github.vim

Put the following code in your ~/.vimrc, you can choose github or molokai (a line starting with a " is a comment):

if &diff
"   colorscheme github
    colorscheme molokai
endif
Max
  • 821
  • 10
  • 11
iceqing
  • 955
  • 11
  • 12
24

I found the easiest way was to paste this one-liner into my ~/.vimrc file:

" Fix the difficult-to-read default setting for diff text highlighting.  The
" bang (!) is required since we are overwriting the DiffText setting. The highlighting
" for "Todo" also looks nice (yellow) if you don't like the "MatchParen" colors.
highlight! link DiffText MatchParen
Geoff Lanotte
  • 7,490
  • 1
  • 38
  • 50
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
24

If you are encountering unreadable color schemes (not just ugly, but unreadable like white text on pink background), an easy fix may be to use 16 colors instead of 256 colors. Then you don't have to mess with the color schemes.

The reason is that the default vimdiff color scheme assigns DiffChange bg as "LightMagenta", which gets mapped to a very light pink in 256 colors. That is unreadable with white text. With 16 colors, the "LightMagenta" is mapped to a bold magenta, which white text shows up much better on.

You can give a quick test by doing something like this:

vimdiff <file1> <file2>
:set t_Co?    " print current setting (256 by default)
:highlight    " print highlighting scheme
:set t_Co=16  " set to 16 colors
:highlight    " print highlighting scheme

256-color screenshot enter image description here

16-color screenshot enter image description here

As you can see, the 16 colors is much more readable, without changing the color scheme.

To make this permanent, you can add set t_Co=16 to your .vimrc

wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • 3
    Thank's a lot for this tip, even though in my case it was the other way round. `t_Co` was set to 8, increasing it to `set t_Co=256` improved readability tremendously. – Olaf Dietsche Oct 20 '18 at 14:27
15

For people that use the very excellent Solarized theme there's an option that turns on high visibility for diff mode:

" ~/vim.rc
" Set high visibility for diff mode
let g:solarized_diffmode="high"

"normal" enter image description here

"high" enter image description here

"low" enter image description here

mandrake
  • 1,213
  • 1
  • 14
  • 28
9

my current colorscheme does not show some diffs properly in vimdiff, For. eg some diff is shown with same fg/bg color

Actually, I've found that the main culprit for same fg/bg color is because of conflict between code syntax highlighting and diff colorscheme. You can try to change the diff colorscheme, but it may be a game of whack-a-mole when you open different file types (with different code syntax highlighting).

A sure solution is to disable the syntax highlighting in vimdiff. You can either type:

:syntax off

Or if you want to automatically do this every time, then add this to the end of your ~/.vimrc:

if &diff
    syntax off
endif
wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • ohw interesting, interestingly [the answer by alan](https://stackoverflow.com/a/13370967/4955266) disables syntax highlighting in the diffed parts i managed to use that to add a different color for each one of unreadable highlights – Fuseteam Dec 16 '20 at 13:40
  • i'm not sure how thorough this will be, but i've now found out, that in the case vim is used in the terminal, the real culprit is vim being unaware of the terminal's background color [Dmitry's answer](https://stackoverflow.com/a/24098619/4955266) seems to fix it in that case – Fuseteam Dec 16 '20 at 13:53
  • This is not working for me for some reason; the syntax stays on. – Carlo Wood Jun 06 '22 at 19:19
7

Another approach is to fix that color scheme.

As far as I know, there are usually four highlight groups relative to diff'ing: DiffAdd, DiffChange, DiffDelete, and DiffText. If you don't want to be bothered about the syntax or tweaking the colors to your liking, you could probably copy your default color scheme under another name to ~/.vim/colors (create the directory if it doesn't exist) and copy paste the corresponding :hi commands from your alternative color scheme to the end of your new custom color scheme, optionnally commenting out any other diff-related statements therein.

And if the result is an obvious improvement, send an email to the maintainer of your color scheme with your changes and ask him to look into the problem. There's a good chance that he will thank you for your interest and that he will fix his color scheme so that other users will also benefit..

guv'
  • 1,359
  • 13
  • 8
  • The vim developers have received numerous complaints and suggestions about how the default colorscheme appears on a dark background, but sadly this has yet been fixed. – Stefan Lasiewski Jun 11 '15 at 21:58
  • it appears the real culprit is vim being unaware that the terminal's background is dark [Dmitry's answer](https://stackoverflow.com/a/24098619/4955266) fixes that – Fuseteam Dec 16 '20 at 13:51
7

/etc/vim/vimrc or ~/.vimrc: If using a dark background within the editing area and syntax highlighting turn on this option as well set background=dark

NicolasMoise
  • 7,261
  • 10
  • 44
  • 65
Dmitry Ershov
  • 96
  • 1
  • 1
7

To expand on @dean and some other answers here, add this to your .vimrc:

if &diff
  " colorscheme evening
  highlight DiffAdd    cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
  highlight DiffDelete cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
  highlight DiffChange cterm=bold ctermfg=10 ctermbg=17 gui=none guifg=bg guibg=Red
  highlight DiffText   cterm=bold ctermfg=10 ctermbg=88 gui=none guifg=bg guibg=Red
endif
dessalines
  • 6,352
  • 5
  • 42
  • 59
4

I use the following when using vimdiff from within vim:

au BufEnter,BufNew * if &diff | syntax off | else | syntax on | endif

The part with else statement is important because that's how you go back to your previous config after you are done with diff'ing. So you can replace syntax off and syntax on with respective colorscheme commands. This autocmd handles changing a setting and reverting it when quitting vimdiff (I use Gdiff to be precise).

mostruash
  • 4,169
  • 1
  • 23
  • 40
1

The slate colorscheme which comes standard with most vim installations works fine for me. FWIW, I work with a dark background. Thus I simply add the following to my .vimrc:

if &diff
  colorscheme slate
endif
firebush
  • 5,180
  • 4
  • 34
  • 45
0

None of the solutions were working for me. When I used the if &diff check, it was only working if I resourced my config after opening the diff (:Gdiff from fugitive.vim plugin). It wasn't opening automatically. Moreover, after quitting the diff pane, I had to resource to get back my original color scheme. Hence, I ended up creating custom maps that would activate the required color scheme.

map ,m :colorscheme molokai<CR>
map ,c :colorscheme PaperColor<CR>
map ,g :colorscheme gruvbox<CR>

So far, this is the most promising solution I found, even though it's a bit of a hack and I would've liked it if the color scheme changed automatically. However, this way, I can apply any color scheme quickly at my leisure irrespective of whether I am in a diff window or not.

Ayush Mandowara
  • 490
  • 5
  • 18