Search code examples
gitvimgit-commitvim-syntax-highlighting

Restrict or change color of specific line in Vim


I use Vim for git commits. I have created a .gitcommittemplate:

############################### <BASLIK> ###############################
#
# ...
#
# |--En fazla 50 Karakter olabilir(Max 50 Char)--|
my real title

my real body

I want to restrict line 6 for 50 characters. Or even better, after 50 characters convert it to red and restrict writing after 72 characters.

How can I do that?


Solution

  • For the highlighting, try this:

    highlight LineSix guifg=red ctermfg=red
    syntax match LineSix /\%6l\%51c.*/
    

    This creates a new highlight group named LineSix, and then sets it to be red in the GUI or in the terminal. Then it says anything matching this regex is that highlight group.

    The regex uses some fun magic. Here's a break down:

    \%6l
    

    will match anything on line 6 (try :h /\%l)

    \%51c
    

    will match anything on column 51 (try :h /\%c)

    and then obviously .* will match anything.

    As for restricting more than 75 characters, I don't think this is possible. You can try setting textwidth and colorcolumn, but these don't force you to use less than 75 characters, and they won't apply to a single line.