Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 +-/*% for c/c++/objc/objcpp. Any vim guru whi can suggest what name i must 'hi' in order to set color for items mentioned?

like image 244
grigoryvp Avatar asked Jul 29 '09 10:07

grigoryvp


4 Answers

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
like image 126
DrAl Avatar answered Nov 04 '22 09:11

DrAl


Get my fork of Rainbow Parentheses Improved (it deals with operators too): https://github.com/oblitum/rainbow

here's a sample of what you can get

like image 27
pepper_chico Avatar answered Nov 04 '22 09:11

pepper_chico


Firstly, get the rainbow colors to working with c, cpp, or whatever lang you desire. which is explained on the download page. http://www.vim.org/scripts/script.php?script_id=1230 , then copy the plugin to a new.vim then vim new.vim and change all the ( and ) to { and } respectively, and save. now redo the getting it working with c, ... (whatever lang) part, but this time change the name to new.vim (or whatever you called it!!) and that worked for me.

the '[' are similar

like image 26
dan Avatar answered Nov 04 '22 07:11

dan


@pepper_chico mentioned rainbow plugin. Many forks now exists for it. The best that I have found is this Rainbow Parantheses Improved. Its actively developed. And it works for many many filetypes rather than just cpp.

Here I will depict installation for Vundle see the link for other ways to install. Vundle plugin install line for this is

Plugin 'luochen1990/rainbow'

Then to install pass the following commands to vim

:so %
:PluginInstall

After this you can add this to your .vimrc for further configuration

let g:rainbow_active = 1 "0 if you want to enable it later via :RainbowToggle
let g:rainbow_conf = {
\   'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick'],
\   'ctermfgs': ['lightblue', 'lightyellow', 'lightcyan', 'lightmagenta'],
\   'operators': '_,\|=\|+\|\*\|-\|\.\|;\||\|&\|?\|:\|<\|>\|%\|/[^/]_',
\   'parentheses': ['start=/(/ end=/)/ fold', 'start=/\[/ end=/\]/ fold', 'start=/{/ end=/}/ fold'],
\   'separately': {
\       '*': {},
\       'tex': {
\           'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/'],
\       },
\       'lisp': {
\           'guifgs': ['royalblue3', 'darkorange3', 'seagreen3', 'firebrick', 'darkorchid3'],
\       },
\       'vim': {
\           'parentheses': ['start=/(/ end=/)/', 'start=/\[/ end=/\]/', 'start=/{/ end=/}/ fold', 'start=/(/ end=/)/ containedin=vimFuncBody', 'start=/\[/ end=/\]/ containedin=vimFuncBody', 'start=/{/ end=/}/ fold containedin=vimFuncBody'],
\       },
\       'html': {
\           'parentheses': ['start=/\v\<((area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr)[ >])@!\z([-_:a-zA-Z0-9]+)(\s+[-_:a-zA-Z0-9]+(\=("[^"]*"|'."'".'[^'."'".']*'."'".'|[^ '."'".'"><=`]*))?)*\>/ end=#</\z1># fold'],
\       },
\       'css': 0,
\   }
\}

Do note the difference in the operators field above and in config in the link I provided. The advanced config given in link only highlights comma , I have edited that to include more operators. Edit that line according to your taste to add some more. This is the result:

Note the change in color because of nesting

like image 36
Siddharth Pant Avatar answered Nov 04 '22 07:11

Siddharth Pant