17

I ran git config --global alias.ci commit --verbose

This makes it so running git ci is like running git commit --verbose

The --verbose flag shows a diff in the commit message template, which is not commented so syntax highlighting works on it, but is automatically recognized to not actually be submitted into the repository's log. Can, of course, be very long, but can be useful for creating better commit comments. (And, if you don't need it, you can just ignore it.)

Anyways, if I run git diff, lines that are removed (start with '-') are in red, and lines that are added (start with '+') are in green.

If I run git ci, vim syntax highlights lines that are removed as regular color (white), and lines that are added as cyan.

How can I make vim syntax highlight show removed lines in red and addes ones in green?

The vim status line says it's editing file "~/code.git/.git/COMMIT_EDITMSG". I don't know much about vim syntax highlighting, but I know it's configurable. I'm not sure how this situation would be configurable, because I assume vim uses file extensions in deciding which syntax highlighting rules to follow (I could be way off here) and git isn't giving this file an extension.

EDIT: Actually, vim must be detecting this is a git commit file, because it's syntax highlighting the first 50 characters as yellow. Assuming that's to indicate what can nicely fit on an emailed subject line for a patch.

1
  • when i ran git diff the -/+ were not red/green. thisi is what i was looking for: git config --global color.ui auto. after setting the configuration, git diff is now the same as giit diff --color
    – WEBjuju
    Jul 14, 2020 at 14:05

3 Answers 3

28

The short version: edit a file ~/.vim/after/syntax/gitcommit.vim, and add to it something like this:

hi diffAdded   ctermfg=green
hi diffRemoved ctermfg=red

The longer version, explaining how I got there:

When you edit a commit message, run :set ft. This will show you that Git commit files have filetype gitcommit. Consequently, to modify highlighting for commit messages you need to edit a file syntax/gitcommit.vim, and since you want your changes to override normal highlighting, you need to put that in an after/ directory. Thus ~/.vim/after/syntax/gitcommit.vim.

Now, to find out what to change. There is this tremendously useful piece of code, that I picked up somewhere (from tpope, IIRC), and that I have been using happily ever after:

nmap <C-S-P> :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
    return
  endif   
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
endfunc

With this, open a verbose commit, go to a removed chunk, and press Ctrl-Shift-P. It will show something like ['gitcommitDiff', 'diffRemoved']. Then go to an added chunk, and get ['gitcommitDiff', 'diffAdded'].

Looking at /usr/share/vim/vim74/syntax/gitcommit.vim shows what's going on: syntax/gitcommit.vim includes syntax/diff.vim, which is the standard highlighting file for diff. Thus the patterns above.

Looking at the end of /usr/share/vim/vim74/syntax/diff.vim, you'll find other patterns you might need to change.

1
  • :set filetype=gitcommit did the trick for me when editing .git/rebase-apply/patch, which for some reason did not seem to be identified as gitcommit automatically.
    – joelpet
    Nov 4, 2015 at 5:42
6

Same as the accepted answer, but some different colours that I find useful for contrast:

hi diffAdded cterm=bold ctermfg=DarkGreen
hi diffRemoved cterm=bold ctermfg=DarkRed

hi diffFile cterm=NONE ctermfg=DarkBlue
hi gitcommitDiff cterm=NONE ctermfg=DarkBlue
hi diffIndexLine cterm=NONE ctermfg=DarkBlue
hi diffLine cterm=NONE ctermfg=DarkBlue
2

Syntax highlighting works when the commit message is defined as gitcommit filetype in the file .vim/filetype.vim:

autocmd BufNewFile,BufRead *.git/COMMIT_EDITMSG set ft=gitcommit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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