Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
520 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...