Say I hava a LaTeX document open in Vim, and I want to highlight every occurence of
{\color{red} ... }
(where the dots are supposed to symbolize some contents), that is, I want to have {\color{red}
, }
and everything between these highlighted. This I have done with
:syn region WarningMsg start=+{\\color{red}+ end=+}+
but I have the problem that, if I write something like {\color{red} some{thing} important}
, then it is only {\color{red} some{thing}
which gets highlighted, because Vim of course mathces the first occurrence of }
. How can I make this Highlighting rule so that it skips matching curly brackets? Even multiple levels of such.
For clarity it is better to give each syntax region a bespoke name, and then link it to a standard colour group. I have renamed your original region
redTeX
.You need to define a second region,
innerBrace
, defining the braces you want to ignore, and mark this region as transparent. ThenredTeX
should be marked to contain the transparent region, which it will then ignore.Note that in this case there is the added subtlety that
redTeX
itself matches as aninnerBrace
. I fixed this by markinginnerBrace
as containingredTeX
.Hope that makes sense!