排除 vim 语法高亮的模式 [英] Excluding the pattern for vim syntax highlighting
问题描述
我正在尝试调整 vim 中的重构文本语法突出显示.我已经尝试了几个 vim 正则表达式来突出显示以下两个示例,但我无法做到.如果我使用搜索/突出显示功能,所有下面的正则表达式都可以完成这项工作,但对于荧光笔(同步匹配)它不起作用.也许我需要将同步匹配更改为其他内容?
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?
这是我在 rst 文件中查看的文本示例:
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.*
当把它放到同步匹配时它不起作用(.vim):
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
推荐答案
当与现有的语法脚本(这里:$VIMRUNTIME/syntax/rst.vim
)集成时,你需要考虑现有的语法组.:syn list
显示所有活动组,但安装 SyntaxAttr.vim - 显示光标下字符的语法高亮属性 插件.(我维护一个扩展叉.)
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.)
在您的示例标题中,我看到 .. item::
部分与 rstExplicitMarkup
匹配,其余部分(您要突出显示的内容)由 rstExDirective
.
On your example headings, I see that the .. item::
part is matched by rstExplicitMarkup
, and the remainder (what you want to highlight) by rstExDirective
.
假设您想集成(而不是完全覆盖)这些,您需要将语法组包含在后者中.这可以通过 containedin=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
.
另一个陷阱是 \zs
限制了突出显示,但在内部仍然匹配整个文本.结合语法高亮,这意味着现有的 rstExplicitMarkup
会阻止您的模式匹配.如果您使用正面回顾 (:help/\@<=
) 相反,它会起作用:
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
这篇关于排除 vim 语法高亮的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!