Excluding the pattern for vim syntax highlighting

后端 未结 1 1050
花落未央
花落未央 2021-01-14 21:45

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.

相关标签:
1条回答
  • 2021-01-14 22:46

    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
    
    0 讨论(0)
提交回复
热议问题