Vim build shows git diff with colors like git diff command (red-delete, green-add)

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

This makes it run git ci

asgit commit --verbose

The flag --verbose

shows a diff in the commit message template, which is not commented out, so syntax highlighting works on it, but is automatically recognized that it is not actually being sent to the repository log. Can be very long, of course, but can be useful for creating more commented comments. (And, if you don't need it, you can just ignore it.)

In any case, if I run git diff

, the lines that are removed (start with '-') are red, and the lines that are added (start with '+') are green.

If I run it git ci

, the vim syntax highlights lines that are removed as normal color (white) and lines that are added as blue.

How can I get the vim syntax to display deleted lines in red and append them in green?

Vim's status bar says that it is editing the file "~ / code.git / .git / COMMIT_EDITMSG". I don't know much about vim syntax highlighting, but I do know that it is customizable. I'm not sure how this situation would be configured as I am assuming that vim uses file extensions when deciding which syntax highlighting rules to follow (I could be here) and git does not give that file an extension.

EDIT: In fact, vim has to detect that it is a git commit file because it syntax highlights the first 50 characters as yellow. Assuming to specify what might fit well in the subject line for the patch.

+3


source to share


3 answers


Short version: edit the file ~/.vim/after/syntax/gitcommit.vim

and add something like this to it:

hi diffAdded   ctermfg=green
hi diffRemoved ctermfg=red

      

Longer version explaining how I got there:

When you edit the commit message, run :set ft

. This will show you that the Git commit files have filetype gitcommit

. Hence, to change the selection for commit messages, you need to edit the file syntax/gitcommit.vim

, and since you want your changes to override the normal selection, you need to put it in a directory after/

. Thus ~/.vim/after/syntax/gitcommit.vim

.

Now to find out what to change. There is this extremely useful piece of code that I took somewhere (from tpope, IIRC) and that I happily use 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 the detail commit, navigate to the deleted snippet and press Ctrl- Shift- P. It will show something like ['gitcommitDiff', 'diffRemoved']

. Then go to the added snippet and get ['gitcommitDiff', 'diffAdded']

.

The window /usr/share/vim/vim74/syntax/gitcommit.vim

shows what's going on: syntax/gitcommit.vim

includes syntax/diff.vim

, which is the default highlight file for diff

. So the templates are above.

At the end /usr/share/vim/vim74/syntax/diff.vim

you will find other templates that you may need to change.

+14


source


Same as the accepted answer, but some different colors 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


source


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

:

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

      

+1


source







All Articles