How to override default syntax highlighting in vim?

In VIM, I need to perform a simple task - to 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 the same commands (without ':' of course) to remove .vimrc and restart VIM - "(" and ")" no longer stand out in .cpp files. It seems that if I create / upload a .cpp file, VIM loads the syntax file for it, which overrides my user flares. How can I tweak the highlights in my .vimrc file so that this happens after the standard syntax definitions or is not affected by the standard syntax definition?

+12
source share
4 answers

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

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

     :help after-directory 
  • Use a match for the current window:

     :match really_unique_name "[()]" 
  • Use matchadd () again for the current window, but this allows you to remove individual matches if you 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) 
  • Install the Dr Chip rainbow.vim plugin to get the highlight in different colors depending on the indentation level.

In this situation, I would recommend option 1, since it looks like you want to make it part of the general syntax. If you want to use matches and want them to be buffer-specific (and not specific to a window), you will 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 a more general operator highlight.

+24
source

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

+9
source

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

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

match has a higher priority than syn-match (i.e., its highlight will override the selection generated by the syn-match), and (correct) syntax files should not interact with it.

The only caveat to coincidence is that it is used for every window, not for every buffer.

If you need additional matches, you can use 2match and 3match.

For more information, see :help :match in Vim.

+4
source

I usually do it like this:

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

au means autocmd . Help will tell more.

+2
source

Source: https://habr.com/ru/post/1304389/


All Articles