如何覆盖vim中的默认语法高亮? [英] How to override default syntax highlight in vim?

查看:40
本文介绍了如何覆盖vim中的默认语法高亮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 VIM 中,我需要执行一个简单的任务 - 突出显示("和)".我可以通过发出两个命令轻松完成此操作:

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

但是如果我添加相同的命令(当然没有 ':')来清空 .vimrc 并重新启动 VIM - "(" 和 ")" 在 .cpp 文件中不再突出显示.似乎如果我创建/加载 .cpp 文件,VIM 会为其加载覆盖我自定义突出显示的语法文件.如何在我的 .vimrc 文件中配置高亮显示,使其在标准语法定义之后发生或不受标准语法定义的影响?

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?

推荐答案

有四个选项(其中两个已被其他人建议):

There are four options (two of which have been suggested by others):

  1. 在 vim 文件中使用 after 结构(~/.vim/after/syntax/cpp.vim):

  1. Use the after structure in vimfiles (~/.vim/after/syntax/cpp.vim):

:help after-directory

  • 对当前窗口使用匹配:

  • Use match for the current window:

    :match really_unique_name "[()]"
    

  • 对当前窗口再次使用 matchadd(),但这允许您在以后需要时删除单个匹配项:

  • Use matchadd(), again for the current window, but this allows you to delete individual matches if you later need to:

    :call matchadd('really_unique_name', "[()]")
    " Or
    :let MyMatchID = matchadd('really_unique_name', "[()]")
    " and then if you want to switch it off
    :call matchdelete(MyMatchID)
    

  • 安装 Dr Chip 的 rainbow.vim 插件以获得大括号突出显示根据缩进级别显示不同的颜色.

  • Install Dr Chip's rainbow.vim plugin to get brace highlighting in different colours depending on the indentation level.

    对于这种情况,我建议您使用选项 1,因为您似乎希望将其作为通用语法的一部分.如果您想使用匹配项并且希望它们是特定于缓冲区的(而不是特定于窗口的),您将需要以下内容:

    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:

    function! CreateBracketMatcher()
        call clearmatches()
        call matchadd('really_unique_name', "[()]")
    endfunc
    au BufEnter <buffer> call CreateBracketMatcher()
    

    有关更多信息,请参阅:

    For more information, see:

    :help after-directory
    :help :match
    :help matchadd()
    :help matchdelete()
    :help clearmatches()
    :help function!
    :help autocmd
    :help autocmd-buffer-local
    :help BufEnter
    

    您可能也对我对这个问题的回答感兴趣,涵盖更一般的运算符突出显示.

    You may also be interested in my answer to this question, which covers more general operator highlighting.

    这篇关于如何覆盖vim中的默认语法高亮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆