views:

859

answers:

3

Hello.

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?

+3  A: 

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.

Laurence Gonsalves
Thanks. It works ok on my windows box. But on MacOS BOX the "hi really_unique_name guifg=#FF0000" is not working. Even if it's an only line in a .vimrc, after starting vim and doing "hi really_unique_name" i see "really_unique_name xxx cleared". Why it's "cleared"?
Eye of Hell
+6  A: 

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

rampion
Hmmm, seems like a good solution. But does not work for me...http://stackoverflow.com/questions/2464593/custumizing-syntax-highlighting-in-vim
sixtyfootersdude
+8  A: 

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.

Al
Thanks for "au BufEnter", works perfectly on both Windows and Macos box (hi do not work on macos box - it is cleared somehow after .vimrc is processed).
Eye of Hell
Buffer local autocmds should only be created after the buffer has been opened. None of the suggestions I listed should be in .vimrc: either use vimfiles/after/syntax/cpp.vim or one of the similar options. Alternatively, you could fudge it with "au BufEnter,BufNew,BufReadPost *.cpp call CreateBracketMatcher()", but it's a bit of a fudge.
Al