Dynamic Syntax Highlighting in Vim

September 20, 2012

linux

I’m a huge fan of syntax highlighting in my editor Vim. One thing I started to
miss was that user defined type miss out on the highlighting, because Vim does
not know about them. Wouldn’t it be cool to have some sort of automatic support that
detect your types and adds them to the correct highlighting group? I call
this “dynamic syntax highlighting”.

As a proof-of-concept I took the tagbar
plugin
, and modified it a little to take advantage of the language detection (specifically the types). The modified code can be found in my fork on github.

It works as follows for Go code. Editing dnssec.go from my Go DNS
repo
.

Here you see a user defined type `rrsigWireFmt` which isn't colored like `uint16`, `uint8`, etc.

Now I fire up :TagbarToggle, which detect the stuff in my file and adds highlighting.

`rrsigWireFmt` is recognized!

There is of course work left, cleaning out stuff from tagbar which I don’t need, supporting multiple files (type defined in X, but you’re editing Y), etc.

Vim