问题
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.
回答1:
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. Then redTeX
should be marked to contain the transparent region, which it will then ignore.
syn region innerBrace start=+{+ end=+}+ transparent contains=redTeX
syn region redTeX start=+{\\color{red}+ end=+}+ contains=innerBrace
hi link redTeX WarningMsg
Note that in this case there is the added subtlety that redTeX
itself matches as an innerBrace
. I fixed this by marking innerBrace
as containing redTeX
.
Hope that makes sense!
来源:https://stackoverflow.com/questions/12880805/syntax-highlighting-between-matching-parenthesis