XPlatformer
XPlatformer

Reputation: 1248

How to "highlight" using text in Vim?

These three lines of Vim script "highlights" all instances of TODO in my source code by placing [[[ and ]]] around it.

:set t_Co=1
:highlight Braces start=[[[ stop=]]]
:call matchadd("Braces", "TODO")

Unfortunately, this only works in "normal terminals". This is why I have to set t_Co to 1.

Can something similar be achieved in a color terminal or in gui? (Or in NeoVim?). This could be an extremely useful way of augmenting your code with meta information.

Upvotes: 1

Views: 2965

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172768

Please keep in mind that Vim is a text editor; these display the file contents mostly as-is. In order to augment your code with meta information, Vim offers:

  • (syntax) highlighting: colors and text attributes like bold or italic
  • concealment: hide or condense matches to nothing / a single character
  • signs (shown in a column left of the window)

Your approach is a hack that misuses the definition of raw terminal codes; these are meant to be invisible control sequences interpreted by the terminal, but you send visible text.

As you've found out at :help highlight-args:

There are three types of terminals for highlighting:
term      a normal terminal (vt100, xterm)
cterm     a color terminal (MS-DOS console, color-xterm, these have the "Co"
          termcap entry)
gui       the GUI

The start= and end= arguments (that your hack relies on) are only supported for "normal terminals", not for cterm and gui. That's why you have to :set t_Co=1 (i.e. force a non-color terminal) for this to work.

Because of these downsides (and more: redrawing problems, vertical navigation that's off), I would recommend to drop this, and use one of the "approved" methods listed above. There are so many Vim users, and they seem to be fine with them as well.

Upvotes: 4

Related Questions