66

I am trying to use vimdiff as my git merge tool, but the colors used are making it unbearable (at least on Windows/Mingw): the background color and the foreground color is the same for some of the conflicting lines, making it needlessly hard to figure out what is going on (see the lines below containing include=).

example

kmario23
  • 353
  • 3
  • 6
oligofren
  • 1,093
  • 1
  • 8
  • 16
  • Just setting 'xterm 256 color' option in terminal will make the colors look better. – balki Jun 23 '23 at 14:22
  • @balki Is that some setting I can universally set on the command line or is this different for every terminal emulator? I use Mac, Linux and WSL2 – oligofren Jun 26 '23 at 08:29

5 Answers5

49

One quick fix is to disable syntax highlighting. Sometimes the code syntax highlighting will cause the foreground text to be the same color as the vimdiff background color, making the text "invisible".

:syntax off

If you want to automatically do this for vimdiff, then add this to the end of your ~/.vimrc:

if &diff
    syntax off
endif
wisbucky
  • 950
  • 8
  • 6
45

The colors are controlled by these four highlight groups (:help hl-DiffAdd):

DiffAdd     diff mode: Added line
DiffChange  diff mode: Changed line
DiffDelete  diff mode: Deleted line
DiffText    diff mode: Changed text within a changed line

These are typically defined by a color scheme, but you can customize them in your ~/.vimrc (after the :colorscheme command) if you like you scheme overall, just not its diff highlighting. Just redefine using :highlight. Here are my personal customizations (for GVIM; for the terminal you need the appropriate ctermfg/bg=... attributes instead / in addition):

hi DiffAdd      gui=none    guifg=NONE          guibg=#bada9f
hi DiffChange   gui=none    guifg=NONE          guibg=#e5d5ac
hi DiffDelete   gui=bold    guifg=#ff8080       guibg=#ffb0b0
hi DiffText     gui=none    guifg=NONE          guibg=#8cbee2

If you're switching colorschemes on the fly, you need to re-invoke those :hi commands via :autocmd ColorScheme * hi ...

Ingo Karkat
  • 17,819
  • 1
  • 45
  • 61
  • 7
    A note for Neovim users confused why these highlight groups aren't working, you have to use diffAdded, diffChanged, and diffRemoved. Not sure what the nvim alternative to DiffText is - maybe diffLine? – J.M. Janzen Apr 28 '20 at 14:07
25

Extending Ingo Karkat's solution to terminal,

hi DiffAdd      ctermfg=NONE          ctermbg=Green
hi DiffChange   ctermfg=NONE          ctermbg=NONE
hi DiffDelete   ctermfg=LightBlue     ctermbg=Red
hi DiffText     ctermfg=Yellow        ctermbg=Red

Below are the cterm-colors, if you want to add your preferred color instead of the ones I used.

       NR-16   NR-8    COLOR NAME
        0       0       Black
        1       4       DarkBlue
        2       2       DarkGreen
        3       6       DarkCyan
        4       1       DarkRed
        5       5       DarkMagenta
        6       3       Brown, DarkYellow
        7       7       LightGray, LightGrey, Gray, Grey
        8       0*      DarkGray, DarkGrey
        9       4*      Blue, LightBlue
        10      2*      Green, LightGreen
        11      6*      Cyan, LightCyan
        12      1*      Red, LightRed
        13      5*      Magenta, LightMagenta
        14      3*      Yellow, LightYellow
        15      7*      White
kmario23
  • 353
  • 3
  • 6
12

Another quick (perhaps even lazy) fix is to just do something like:

:colo desert

This will change your color scheme, and in some cases will make hidden text become visible.

Nathan Chappell
  • 221
  • 2
  • 4
2

Accepted answer didn't work for me. I'm using vim 8.2. Add to .vimrc after 'colorscheme ...'

colorscheme challenger_deep

" Diff colors (Stand with Ukraine). hi DiffAdded ctermfg=Yellow ctermbg=NONE hi DiffRemoved ctermfg=Blue ctermbg=NONE

Changes against accepted answer:

enter image description here

VladSavitsky
  • 121
  • 4
  • 1
    Are you sure this is correct? These names are hard-coded in Vim, and have ever since they're introduced. I can't really see how/why this works, unless that colourscheme does something very odd – Martin Tournoij Jun 23 '23 at 14:36
  • @MartinTournoij I'm not sure how it works but it really works in my version of vim. Colorscheme do not add new color groups. Using 'DiffAdd' do not work but 'DiffAdded' works for me. – VladSavitsky Jun 24 '23 at 20:56
  • Do you have a link to your colour scheme? – Martin Tournoij Jun 24 '23 at 21:03
  • Sure. Here it is: https://github.com/challenger-deep-theme/vim – VladSavitsky Jun 25 '23 at 06:44
  • Will be interesting to figure out how this works – oligofren Jun 26 '23 at 08:28
  • Ooo, thanks! My read on the situation: DiffAdd and friends have long been under vimdiff to highlight differences between buffers. Recently, I saw DiffAdd also start getting used under set syntax=diff to highlight lines starting with + and -. But I want the two to be different. So I kept my existing DiffAdd setting, and I changed DiffAdded (which I saw was being used under set syntax=diff, thanks to syn list, per https://stackoverflow.com/a/29168584/28465) to what I want there. (It's possible that things differ further across color schemes. I'm using a modified desert256.) – Chris Povirk Jan 10 '24 at 22:18
  • (And I guess that's controlled by https://github.com/vim/vim/blob/71d0ba07a33a750e9834cd42b7acc619043dedb1/runtime/syntax/diff.vim#L334-L338 and https://github.com/vim/vim/blob/71d0ba07a33a750e9834cd42b7acc619043dedb1/runtime/syntax/diff.vim#L383? But that file changed recently, including parts related to diffs. So I'm probably actually seeing an older version.) – Chris Povirk Jan 10 '24 at 22:22