如何覆盖 vim 中的默认语法高亮?
在 VIM 中,我需要执行一个简单的任务 - 突出显示“(”和“)”。 我可以通过发出两个命令轻松地做到这一点:
:syn match really_unique_name display "[()]"
:hi really_unique_name guifg=#FF0000
但是如果我将相同的命令(当然没有':')添加到空.vimrc并重新启动VIM - “(”和“)”在.cpp文件中不再突出显示。 看来,如果我创建/加载 .cpp 文件,VIM 会为其加载语法文件,从而覆盖我的自定义突出显示。 我如何在 .vimrc 文件中配置突出显示,以便它在标准语法定义之后发生,或者不受标准语法定义的影响?
In VIM, I need to perform a simple task - highlight "(" and ")". I can do this easily by issuing two commands:
:syn match really_unique_name display "[()]"
:hi really_unique_name guifg=#FF0000
But if I add same commands (without ':' of course) to empty .vimrc and restart VIM - "(" and ")" are not highlighted anymore in .cpp files. It seems that if i create/load .cpp file, VIM loads syntax file for it that overrides my custom highlights. How can i configure highlights in my .vimrc file so it will take place after standard syntax definitions or will not be affected by standard syntax definition?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于那些想要覆盖
hi def link
声明的人,请使用hi! 默认链接
For those who wants to overwrite a
hi def link
declaration, usehi! def link
我通常这样做:
au
代表autocmd
。 帮助会告诉你更多。I usually do it like this:
au
stands forautocmd
. Help will tell more.不使用 syn match,只需使用 match。 例如:
match
比 syn-match 具有更高的优先级(即:它的突出显示将覆盖 syn-match 生成的突出显示),并且(行为良好的)语法文件不应与它混淆。需要注意的是,匹配是针对每个窗口的,而不是针对每个缓冲区的。
如果您需要额外的匹配,您可以使用 2match 和 3match。
请参阅 Vim 中的
:help :match
了解更多信息。Instead of using syn match, just use match. eg:
match
has higher precedence than syn-match (ie: its highlighting will override the highlighting generated by syn-match), and (well-behaved) syntax files should not mess with it.The one caveat with match is that it's per-window, rather than per-buffer.
If you need additional matches you can use 2match and 3match.
See
:help :match
in Vim for more info.将设置放在 ~/.vim/after/syntax/cpp.vim 中
Put the settings in ~/.vim/after/syntax/cpp.vim
有四个选项(其中两个已由其他人建议):
在 vim 文件中使用
after
结构 (~/.vim/after/syntax/cpp.vim):对当前窗口使用匹配:
对当前窗口再次使用 matchadd(),但这允许您在以后需要时删除单个匹配:
安装 Dr Chip 的 rainbow.vim 插件,根据缩进级别以不同颜色突出显示大括号。
对于这种情况,我建议使用选项 1,因为看起来您希望将其作为通用语法的一部分。 如果您想使用匹配并且希望它们特定于缓冲区(而不是特定于窗口),您将需要类似以下内容:
有关更多信息,请参阅:
您可能还对我对 这个问题,涵盖了更一般的运算符突出显示。
There are four options (two of which have been suggested by others):
Use the
after
structure in vimfiles (~/.vim/after/syntax/cpp.vim):Use match for the current window:
Use matchadd(), again for the current window, but this allows you to delete individual matches if you later need to:
Install Dr Chip's rainbow.vim plugin to get brace highlighting in different colours depending on the indentation level.
For this situation, I'd recommend option 1 as it looks like you want to make it part of the general syntax. If you want to use matches and you want them to be buffer specific (rather than window specific), you'll need something like:
For more information, see:
You may also be interested in my answer to this question, which covers more general operator highlighting.