← archive

Git commit message syntax highlighting in Vim

March 20, 2018
vim git

Although you could use git commit -m for writing a git commit message, it's much better to use a proper editor for that, especially if you're writing a multiline commit message.

Well if your editor of choice is Vim, this is what happens after you enter git commit.

Notice how first 50 chars of a commit message are highlighted in red. This is used to notify you about the length of the first line of a commit message so you know when it's too lengthy. For Vim, 50 chars is the suggested size of the first line of commit message.

Well this isn't a good default for me, since the only thing that's worrying me is the truncation of the first line of commit message when my code ends up on Github. I'm only interested if the first line of my commit message is short enough to be displayed in full on Github.

If that's not the case I'll end up with something like this.


Preventing this is easy. Just make Vim highlight the first 72 chars in red, which is the maximum number of characters Github allows in the first line.

In order to do so, alter Vim's syntax rules for git commit messages, by editing ~/.vim/after/syntax/gitcommit.vim like this.

syn clear gitcommitSummary
syn match gitcommitSummary    "^.\{0,72\}" contained containedin=gitcommitFirstLine nextgroup=gitcommitOverflow contains=@Spell

This will make Vim look like this.

Now you'll be notified if you cross the length limit for the first line of git commit message on Github and you'll know exactly where it starts truncating it.

@shime_sh Hrvoje Šimić
0 kudos
I write every day, but I no longer publish everything. Join my sporadic newsletter to never miss an article.

Unsubscribe anytime. By submitting, you agree to our privacy policy.