在vim中为c ++着色的大括号和运算符?

时间:2009-07-29 10:28:04

标签: vim syntax-highlighting

我想在vim中为c ++自定义语法着色。但是,不幸的是,我仍然找不到braces(){} []和c + c ++ / objc / objcpp的运算符+ - / *%的正确名称。任何vim大师谁可以建议我必须'hi'什么名称,以便为所提到的项目设置颜色?

4 个答案:

答案 0 :(得分:28)

我认为在C代码或衍生语言的vim中没有默认的大括号作为标准(它们只是以纯文本形式突出显示)。您可以使用以下内容定义自己的内容:

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

或者您可以下载rainbow brace highlighting plugin,它为不同级别的缩进提供不同的颜色。另请参阅我对this question的回答。

:help :syn-match
:help hi

彩虹支架荧光笔的屏幕截图(使用我的Bandit配色方案)here

修改

为了找出您感兴趣的任何突出显示组,请创建此映射:

: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>

(摘自here)。然后,将光标移动到您感兴趣的任何内容上,然后按F3。如果它没有突出显示,Vim将打印:

hi<> trans<> lo<>

如果有特定的突出显示组,您将得到类似的内容(将光标放在if关键字上):

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

告诉您突出显示组名为cConditional,并且已与名为:hi link的组关联(使用Conditional)。使用彩虹支撑突出显示,您可能会得到类似cCurly1的内容,这意味着它位于大括号内,但没有其他突出显示。

编辑2:

可能的操作员匹配器(未经过很好的测试):

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

答案 1 :(得分:7)

让我的彩虹圆括号改进(它也与运营商打交道):https://github.com/oblitum/rainbow

here's a sample of what you can get

答案 2 :(得分:1)

首先,让彩虹的颜色与c,cpp或你想要的任何东西一起工作。这在下载页面上有说明。 http://www.vim.org/scripts/script.php?script_id=1230, 然后将插件复制到new.vim 然后vim new.vim并分别将所有(和)更改为{和}, 并保存。 现在重做它使用c,...(无论lang)部分,但这次将名称更改为new.vim(或任何你称之为!!),这对我有用。

'['相似

答案 3 :(得分:1)

@pepper_chico提到了彩虹插件。现在有许多叉子存在。我发现的最好的是Rainbow Parantheses Improved。它积极发展。它适用于许多文件类型而不仅仅是cpp。

这里我将描述Vundle的安装,请参阅其他安装方式的链接。 Vundle插件安装行是

Plugin 'luochen1990/rainbow'

然后安装将以下命令传递给vim

:so %
:PluginInstall

在此之后,您可以将其添加到.vimrc以进行进一步配置

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,
\   }
\}

请注意我提供的链接中上面和配置中的运算符字段的区别。链接中给出的高级配置仅突出显示逗号,我已编辑该命令以包含更多运算符。根据您的喜好编辑该行以添加更多。这是结果:

Note the change in color because of nesting

相关问题