Vim recipe for a minor syntax highlighting extension


I want to leave my system’s Python syntax highlighting mostly intact, but I have a specific pattern I’d like to highlight for an idiom I use a lot. How can I add additional highlighting instructions on top of the existing highlighting done by vim?

(Apologies if this has already been asked. All the vim syntax highlighting questions I found seemed to involve writing a new syntax highlighting from scratch.)


Put your additional :syntax commands into ~/.vim/after/syntax/python.vim, and they will be automatically executed after the original syntax script.

  • It’s easy to highlight stuff that so far isn’t parsed at all.
  • For elements already parsed / hightlighted, you need to find out by which syntax group (e.g. pythonFunction), and add a containedin=pythonFunction clause to your :syntax commands. Without that, the original matching will obscure yours. To find out which syntax group causes the highlighting. :syn list shows all active groups, but it’s easier when you install the SyntaxAttr.vim – Show syntax highlighting attributes of character under cursor plugin.
  • Introducing highlighting across (larger) elements that have multiple existing syntax groups is difficult, as your match will obscure the original ones, and that may break the entire parsing. You need to carefully examine the existing nested element structure, and try to fit in yours, again via contains= and containedin= clauses. Depending on the actual situation, that can be difficult.

For the actual syntax definitions, see the help starting at :h :syn-keyword. Basically, there are simple keyword definitions, regular expression matches, and regions defined by start and end patterns.

Answered By – Ingo Karkat

Answer Checked By – Katrina (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.