vimsyntax-highlighting

Braces and operators coloring in Vim for C++?


I want to customize a syntax coloring in Vim for C++. But, unfortunately, I still can't find a correct name for braces (){}[] and operators like +-/*% for C/C++ and Objective C/C++. Any Vim guru which can suggest what name I must :hi in order to set color for items mentioned?


Solution

  • I believe that there is no default highlighting for braces as standard in vim for C code or derivative languages (they're just highlighted as plain text). You could define your own, using something like:

    :syn match Braces display '[{}()\[\]]'
    :hi Braces guifg=red
    

    or you could download the rainbow brace highlighting plugin, which gives varying colours for different levels of indentation. See also my answer to this question.

    :help :syn-match
    :help hi
    

    There is a screenshot of the rainbow brace highlighter in action (with my Bandit colour scheme) here.

    Edit:

    In order to find out the highlighting group of anything that interests you, create this mapping:

    :map <F3> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
    

    (taken from here). Then, move the cursor over whatever you're interested in and press F3. If it's not highlighted at all, Vim will print:

    hi<> trans<> lo<>
    

    If there's a particular highlight group, you'll get something like this (with the cursor over the if keyword):

    hi<cConditional> trans<cConditional> lo<Conditional>
    

    which tells you that the highlight group is called cConditional and that it is linked (with :hi link) to the group called Conditional. With rainbow brace highlighting, you may get something like cCurly1, which means it's inside a curly brace, but with no additional highlighting.

    Edit 2:

    A possible operator matcher (not very well tested):

    let cOperatorList  = '[-&|+<>=*/!~]'    " A list of symbols that we don't want to immediately precede the operator
    let cOperatorList .= '\@<!'             " Negative look-behind (check that the preceding symbols aren't there)
    let cOperatorList .= '\%('              " Beginning of a list of possible operators
    let cOperatorList .=     '\('           " First option, the following symbols...
    let cOperatorList .=        '[-&|+<>=]'
    let cOperatorList .=     '\)'
    let cOperatorList .=     '\1\?'         " Followed by (optionally) the exact same symbol, so -, --, =, ==, &, && etc
    let cOperatorList .= '\|'               " Next option:
    let cOperatorList .=     '->'           " Pointer dereference operator
    let cOperatorList .= '\|'               " Next option:
    let cOperatorList .=     '[-+*/%&^|!]=' " One of the listed symbols followed by an =, e.g. +=, -=, &= etc
    let cOperatorList .= '\|'               " Next option:
    let cOperatorList .=     '[*?,!~%]'     " Some simple single character operators
    let cOperatorList .= '\|'               " Next option:
    let cOperatorList .=     '\('           " One of the shift characters:
    let cOperatorList .=         '[<>]'     
    let cOperatorList .=     '\)'
    let cOperatorList .=     '\2'           " Followed by another identical character, so << or >>...
    let cOperatorList .=     '='            " Followed by =, so <<= or >>=.
    let cOperatorList .= '\)'               " End of the long list of options
    let cOperatorList .= '[-&|+<>=*/!~]'    " The list of symbols that we don't want to follow
    let cOperatorList .= '\@!'              " Negative look-ahead (this and the \@<! prevent === etc from matching)
    
    exe "syn match cOperator display '" . cOperatorList . "'"
    
    syn match cOperator display ';'
    hi link cOperator Operator