Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override default syntax highlight in 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

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?

like image 806
grigoryvp Avatar asked Jul 31 '09 09:07

grigoryvp


People also ask

How do I enable syntax highlighting in vim by default?

Syntax highlighting is on for vim editor by default. The content of login.sh will be displayed with the following format when the syntax highlighting is on. After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting.

How do I enable syntax highlighting in vi?

You can Turn On or Turn Off syntax highlighting by pressing ESC button and use command as :syntax on and :syntax off in Vi editor.

How do you highlight in vim?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.


1 Answers

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.

like image 154
DrAl Avatar answered Sep 26 '22 22:09

DrAl