Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get normal diff colors in Vim on verbose git commits?

When running the normal git diff I get the intuitive colors that I'm used to:

git diff output with red/green/white font color.

Red for deleted, green for added, white for unchanged.

But when I run git commit -v, the included diff that opens in the editor has weird colors that I'm not used to, and so parsing the meaning of the diff is more difficult:

git commit -v output with unhelpful colors.

White/gray for deleted, cyan for added, white for unchanged? What is that?

What can I do so that the diff inside the editor on verbose commits has the same colors as when running git diff directly?

I am using MacOS and vim as editor (git config --global core.editor /usr/bin/vim)

like image 213
talz Avatar asked Oct 17 '25 12:10

talz


1 Answers

git commit -v opens a file named COMMIT_EDITMSG. Vim (8.2) will set the filetype of a file with this name to gitcommit.

The corresponding syntax rules ($VIMRUNTIME/syntax/gitcommit.vim) reference the syntax/diff.vim rules, which make the lines starting with + and - into diffAdded and diffRemoved syntax items, respectively.

You need to set up your colorscheme and/or terminal colors so that diffAdded looks green and diffRemoved looks red.


Check with :hi diffAdded and :hi diffRemoved why they are currently not green and red.

e.g. you may see

:hi diffAdded
diffAdded      xxx links to Identifier

and then

:hi Identifier
Identifier     xxx term=underline cterm=bold ctermfg=6 guifg=palegreen

To override the specific highlight groups relevant to verbose Git commits, one might add something like this to their .vimrc:

function! MyHighlights() abort
    highlight diffAdded ctermfg=2 guifg=#67c12c
    highlight diffRemoved ctermfg=1 guifg=#b82e19
endfunction
augroup MyColors
    autocmd!
    autocmd ColorScheme * call MyHighlights()
augroup END

If you don't already specify a colorscheme, you also need to add this to trigger the ColorScheme event:

colorscheme default
like image 166
mkrieger1 Avatar answered Oct 20 '25 03:10

mkrieger1



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!