How to override default syntax highlight in vim?

本小妞迷上赌 提交于 2019-11-28 21:34:13
DrAl

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

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

    :help after-directory
    
  2. Use match for the current window:

    :match really_unique_name "[()]"
    
  3. 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)
    
  4. 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:

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.

Put the settings in ~/.vim/after/syntax/cpp.vim

Instead of using syn match, just use match. eg:

hi really_unique_name guifg=#FF0000
match really_unique_name "[()]"

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.

Peter

I usually do it like this:

:hi really_unique_name guifg=#FF0000
:au BufNewFile,BufRead * :syn match really_unique_name display "[()]"

au stands for autocmd. Help will tell more.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!