问题
I am trying to adjust the reStructured text syntax highlighting in vim. I have tried several vim regexes to get highlight working for below two examples, but I am unable to. If I use search/highlight function all below regexes do the job, but for highlighter (syn match) it is not working. Maybe I need to change syn match to something else?
This is the text example I am looking at in rst file:
.. item:: This is the title I want to highlight
there is some text here which I do not care
.. item-matrix:: This is the title I want to highlight
:source: XX
:target: YY
Regexes that match the text:
[.+].*[:+] \zs.*
\(.. .*:: \)\zs.*
When putting that to syn match it does not work (.vim):
syn match rstHeading /[.+].*[:+] \zs.*/
I know I am close because above example matches for
..:: This is highlighted as rstHeading
回答1:
When integrating with an existing syntax script (here: $VIMRUNTIME/syntax/rst.vim
), you need to consider the existing syntax groups. :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. (I maintain an extended fork.)
On your example headings, I see that the .. item::
part is matched by rstExplicitMarkup
, and the remainder (what you want to highlight) by rstExDirective
.
Assuming that you want to integrate with (and not completely override) these, you need your syntax group to be contained inside the latter. This can be done via containedin=rstExDirective
.
Another pitfall is that \zs
limits the highlighting, but internally still matches the whole text. In combination with syntax highlighting, this means that the existing rstExplicitMarkup
prevents a match of your pattern. If you use a positive lookbehind (:help /\@<=) instead, it'll work:
syn match rstHeading /\%([.+].*[:+] \)\@<=.*/ containedin=rstExDirective
Of course, to actually see any highlighting, you also need to define or link a highlight group to your new syntax group:
hi link rstHeading Title
来源:https://stackoverflow.com/questions/53727694/excluding-the-pattern-for-vim-syntax-highlighting