Making vim show a git diff with colors like a git diff command (red-delete, green-add)
git config --global alias.ci commit --verbose
This makes it so running
git ci is like running
git commit --verbose
--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.
3 Solutions collect form web for “Making vim show a git diff with colors like a git diff command (red-delete, green-add)”
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
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
/usr/share/vim/vim74/syntax/gitcommit.vim shows what’s going on:
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.
Syntax highlighting works when the commit message is defined as gitcommit filetype in the file
autocmd BufNewFile,BufRead *.git/COMMIT_EDITMSG set ft=gitcommit
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