2

I have a terminal color scheme that makes the default highlighting settings for diffs relatively hard to read. To address this, I’ve added my own custom settings inside of ~/.vim/after/syntax/diff.vim:

hi diffAdded term=NONE ctermfg=Green 
hi diffRemoved term=NONE ctermfg=Red

This works great for git diff, for example, but the customizations do not apply when I use git commit -v, so I still get unreadable diffs. I looked in the syntax definition for gitcommit.vim, and I noticed that it uses syn include to create a nested region:

" gitcommit.vim
syn include @gitcommitDiff syntax/diff.vim
syn region gitcommitDiff start=/\%(^diff --\%(git\|cc\|combined\) \)\@=/ end=/^\%(diff --\|$\|#\)\@=/ fold contains=@gitcommitDiff

Since it looks like it just uses diff.vim under the hood, I figured that copying my custom styles to ~/.vim/after/syntax/gitcommit.vim would do the trick:

" identical to diff.vim
hi diffAdded term=NONE ctermfg=Green
hi diffRemoved term=NONE ctermfg=Red

Unfortunately, this doesn’t seem to work—vim still uses the default syntax highlighting.

What’s wrong? How can I customize the syntax highlighting for a region included with syn include?

3
  • :syntax defines, well, syntax. It says "if text matches this regular expression, it's a string", and so forth. To do so, it assigns a highlight group. It doesn not say anything about colours.
  • :highlight says "highlight this group with these colours".

Whatever :syntax does is fairly irrelevant if you just want to change some colours. All we need to know is the correct highlight group that is assigned.

This is also how colour schemes work. Colour schemes are nothing more than a set of highlight statements for a bunch of standard groups (String, Identifier, etc.)

There is no need to put highlight statements in ~/.vim/after/syntax/diff.vim, since syntax files don't define these. This is only required for :syntax rules.1 There is also no relation between the diff syntax being used with syn include and nested and your custom highlight not working.

Simply adding:

highlight diffAdded ctermbg=green
highlight diffRemoved ctermbg=red

to your vimrc should work.

Note that highlights are reset when setting colour schemes So I typically do:

fun! s:color()
    highlight diffAdded ctermbg=green
    highlight diffRemoved ctermbg=red
endfun
call s:color()
augroup my_colors
    autocmd!
    autocmd Colorscheme * call s:color()
augroup end 

To make sure the custom highlights are always applied.

Perhaps this is the reason it doesn't work for you?


1: There is one exceptions to this: when you want to link a highlight group to another highlight group. But this is rare.

  • Your explanation makes sense to me, and I was quite surprised that my original attempt didn’t work. Unfortunately, your suggestion didn’t seem to fix things. Is there some way that I could check what highlight group is being used under the cursor or something along those lines? Even running hi diffAdded term=NONE ctermfg=Green directly while editing a commit message doesn’t seem to do anything. – Alexis King Apr 5 '16 at 16:13
  • @AlexisKing Sure, this should give you the highlight group name: :echo synIDattr(synID(line('.'), col('.'), 1), 'name')... I tried this with git commit -v by the way, and it works fine for me ... Not sure why it doesn't for you. You can try following the stepts in How do I debug my vimrc file? – Martin Tournoij Apr 5 '16 at 16:19
  • Weird! After some more inspection, it seems to actually be working, but for some reason the Green color renders differently when I run git commit -v and git diff. Anyway, that’s a separate problem, so I will continue to investigate it myself. Sorry for the small XY problem, and thanks for your help! – Alexis King Apr 5 '16 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.