Home > database >  Colorizing custom gitmessage file in vim
Colorizing custom gitmessage file in vim

Time:02-02

I have a custom .gitmessage. It is defined in the git config. I've added a ~/.vim/after/ftplugin/gitcommit.vim to redefine a couple of things in the plugin (in support of Conventional Commits), e.g., textwidth.

My .gitmessage looks something like this:



#subject-|---------|---------|---------|---------|---------|---------|---------|---------|-------->|
# Commit messages should follow the Conventional Commits Specification
# <type>(<optional scope>): <imperative subject>
# empty separator line
# <optional body>
# empty separator line
# <optional footer>
#
# API relevant changes                              || perf     commits improve performance
#   feat commits add a new feature                  || revert   commit, well, revert a previous 
#   fix  commits fix a bug                          ||          commit
...
...

where I provide an ASCII "ruler" for 100 chars (though the VIM filetype plugin config autowraps to the textwidth per my customization, &tc.

However, I would like to colorize the conventional commit sample and the type keywords, e.g., feat, fix, perf, &tc.

I've tried most of standard ASCII escape codes in the .gitmessage, e.g., \\e[1;30m, ^[1;30m (Ctrl-V, esc), and \033[1;30m. Alas, they all just get dumped to the vim editor—it makes sense that vim would make sure that these codes are editable—but gitcommit.vim does colorize the diff in git commit --all --verbose...

This appears to be, maybe, a function of shellescape (see :h shellescape in vim). I see this used in some of the filetype plugins, but I'm not a plugin hacker.

Any pointers?

CodePudding user response:

Vim doesn't care about your escape codes.

A quick and dirty method would be to use :help :match or :help matchadd() in your custom ftplugin:

:match

But the real solution is to create your own syntax script, following the instructions under :help usr_44. See $VIMRUNTIME/syntax/gitcommit.vim for a start.

  • Related