How to override default syntax highlight in vim?

喜夏-厌秋 提交于 2019-11-27 13:57:30

问题


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?


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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.



来源:https://stackoverflow.com/questions/1211588/how-to-override-default-syntax-highlight-in-vim

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