Highlighting arbitrary lines in Vim

Setting up highlight groups

As part of the code I'm writing for my thesis, I'd like to be able to highlight arbitrary lines of source code in Vim. I did this by defining a match group and then setting up highlighting for that match group, using the technique described in this Vim FAQ entry (How do I highlight all the characters after a particular column?).

Before trying to match things you need to specify how they're highlighted. This defines a new highlight type called "currawong":

:highlight currawong ctermbg=darkred guibg=darkred

To set up a match group named "currawong" that highlights line 7:

:match currawong /\%7l/

Highlighting multiple lines is done almost as expected - you have to escape the "or". Here's how to highlight lines 7 and 10:

:match currawong /\%7l\|\%10l/

Helpful portions of the Vim manual are pattern (:help pattern) and match (:help match).

The major problem with this

Although Vim will update the highlighting for arbitrary characters, it doesn't update the highlight for matched lines when lines are added or deleted, which is a complete pain and effectively means you need to re-define your match groups every time that happens.

Because I'm not exactly doing regular syntax highlighting, it's probably acceptable to do this as an autocommand that runs when the file is loaded or when the programmer exits insert mode (:help :au wrt InsertLeave and BufRead)

Index