大括号和运算符在 vim 中为 C++ 着色? [英] braces and operators coloring in vim for c++?
问题描述
我想在 vim 中为 C++ 自定义语法着色.但是,不幸的是,我仍然无法为 c/c++/objc/objcpp 找到大括号 (){}[] 和运算符 +-/*% 的正确名称.任何 vim 大师 whi 都可以建议我必须嗨"什么名字才能为提到的项目设置颜色?
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?
推荐答案
我相信在 vim 中,对于 C 代码或派生语言,没有标准的大括号默认高亮显示(它们只是作为纯文本高亮显示).您可以使用以下内容定义自己的:
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
有一个彩虹支架荧光笔的屏幕截图(使用我的 Bandit 配色方案)此处
There is a screenshot of the rainbow brace highlighter in action (with my Bandit colour scheme) here.
为了找出您感兴趣的任何事物的突出显示组,请创建此映射:
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>
(取自此处).然后,将光标移到您感兴趣的任何内容上并按 F3.如果它根本没有突出显示,Vim 将打印:
(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
关键字上):
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>
它告诉你高亮组被称为 cConditional
并且它被链接(使用 :hi link
)到被称为 Conditional
的组.使用彩虹大括号突出显示,您可能会得到类似 cCurly1
的内容,这意味着它位于大括号内,但没有额外的突出显示.
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.
编辑 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
这篇关于大括号和运算符在 vim 中为 C++ 着色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!