C++ Vim 中突出显示的类和函数名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/736701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
class & function names highlighting in Vim
提问by ivanTheTerrible
I just recently set up my Vim environment from Textmate, after becoming addicted to its modal input.
在对它的模态输入上瘾之后,我最近刚刚从 Textmate 设置了我的 Vim 环境。
However, syntax highlighting seems to be not so beautiful in Vim. I code in C++ and since the function call and class names can't be highlighted, the code is more difficult to read. I played with color scheme for a bit, but couldn't find any field that corresponded to "class name" or "function name".
然而,语法高亮在 Vim 中似乎没有那么漂亮。我用 C++ 编写代码,由于无法突出显示函数调用和类名,因此代码更难阅读。我玩了一会儿配色方案,但找不到与“类名”或“函数名”对应的任何字段。
In the picture below, notice how DroughtLayer::
and *.size()
is not highlighted on the right in MacVim.
在下图中,请注意MacVim 右侧的DroughtLayer::
和*.size()
未突出显示。
(source: ivzhao.com)
(来源:ivzhao.com)
Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.
任何想法如何解决这个问题?这真的让我很恼火,因为我是一个视觉敏感的人。
采纳答案by thomasrutter
Interestingly, the syntax highlighters in VIM don't support applying a syntax to identifiers or function names - at least not the syntax highlighters for C and C++. So, even if you do:
有趣的是,VIM 中的语法高亮器不支持将语法应用于标识符或函数名称——至少不支持 C 和 C++ 的语法高亮器。所以,即使你这样做:
:hi Function guifg=red
or
或者
:hi Identifier guifg=red
it doesn't give these a color. I just seems to be not much more than keywords and constants for these languages.
它没有给这些颜色。我似乎只是这些语言的关键字和常量。
Here, someone has started extending the cpp syntax file to support method names. It's a start I guess. http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
在这里,有人开始扩展 cpp 语法文件以支持方法名称。我想这是一个开始。 http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
回答by Eduardo
I had this very same problem when I started using vim. The solution is simple, you just have to edit the c syntax file used by vim, here's how to do it:
当我开始使用 vim 时,我遇到了同样的问题。解决方法很简单,你只需要编辑vim使用的c语法文件,方法如下:
When you start editing a C or C++ file, vim reads the default c syntax file located in
当您开始编辑 C 或 C++ 文件时,vim 会读取位于
$VIMRUNTIME/syntax/c.vim
(Where $VIMRUNTIME is where you have vim installed. You can find out it's default value by opening vim and using the command ":echo $VIMRUNTIME").
($VIMRUNTIME 是你安装 vim 的地方。你可以通过打开 vim 并使用命令 ":echo $VIMRUNTIME" 找到它的默认值)。
You can simply overwrite that file, or you can create your custom C syntax file (which will be loaded by vim instead of the default one) in this location:
你可以简单地覆盖那个文件,或者你可以在这个位置创建你的自定义 C 语法文件(它将被 vim 加载而不是默认的):
$HOME/.vim/syntax/c.vim (for UNIX)
$HOME/vimfiles/syntax/c.vim (for PC or OS/2)
(I have never used a Mac so I dunno which one will work for you. You can find out more in the vim help, ":help vimfiles")
(我从来没有用过 Mac,所以我不知道哪一个适合你。你可以在 vim 帮助中找到更多信息,“:help vimfiles”)
Now the fun part. Copy the default "$VIMRUNTIME/syntax/c.vim" file to your vimfiles directory ("$HOME/.vim/syntax/c.vim" for UNIX), and edit it by adding these lines:
现在是有趣的部分。将默认的 "$VIMRUNTIME/syntax/c.vim" 文件复制到你的 vimfiles 目录(UNIX 的 "$HOME/.vim/syntax/c.vim"),并通过添加以下行来编辑它:
" Highlight Class and Function names syn match cCustomParen "(" contains=cParen,cCppParen syn match cCustomFunc "\w\+\s*(" contains=cCustomParen syn match cCustomScope "::" syn match cCustomClass "\w\+\s*::" contains=cCustomScope hi def link cCustomFunc Function hi def link cCustomClass Function
" Highlight Class and Function names syn match cCustomParen "(" contains=cParen,cCppParen syn match cCustomFunc "\w\+\s*(" contains=cCustomParen syn match cCustomScope "::" syn match cCustomClass "\w\+\s*::" contains=cCustomScope hi def link cCustomFunc Function hi def link cCustomClass Function
That's it! Now functions and class names will be highlighted with the color defined in the "Function" highlight (":hi Function"). If you want to customize colors, you can change the last two lines above to something like this:
就是这样!现在,函数和类名称将使用“函数”突出显示(“:hi 函数”)中定义的颜色突出显示。如果要自定义颜色,可以将上面的最后两行更改为如下所示:
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def cCustomClass gui=reverse guifg=#00FF00
or you can leave the C syntax file alone and define colors in your vimrc file (":help vimrc"):
或者您可以单独使用 C 语法文件并在您的 vimrc 文件中定义颜色 (":help vimrc"):
hi cCustomFunc gui=bold guifg=yellowgreen
hi cCustomClass gui=reverse guifg=#00FF00
(Note the absence of the "def" keyword, go to ":help highlight-default" for details). For the available parameters to the ":hi" command see ":help :highlight".
(注意没有“def”关键字,详情请到“:help highlight-default”)。":hi" 命令的可用参数见 ":help :highlight"。
You can find the complete c.vim file for Vim 7.2 on this link (Note: only use this if you have a non-modified Vim, version 7.2):
您可以在此链接上找到 Vim 7.2 的完整 c.vim 文件(注意:只有在您拥有未修改的 Vim 7.2 版时才使用此文件):
And the obligatory screenshot:
以及强制性截图:
回答by Janosimas
this is my first post here and i didn't know how to make an observation, the answer of Eduardo makes "(" and "{" look unmached and bugs syntax foldind, I changed it a little to fix this.
这是我在这里的第一篇文章,我不知道如何进行观察,Eduardo 的回答使 "(" 和 "{" 看起来没有任何改动并且错误语法折叠,我对其进行了一些更改以解决此问题。
syn match cCustomParen "?=(" contains=cParen,cCppParen
syn match cCustomFunc "\w\+\s*(\@=" contains=cCustomParen
syn match cCustomScope "::"
syn match cCustomClass "\w\+\s*::" contains=cCustomScope
hi def cCustomFunc gui=bold guifg=yellowgreen
hi def link cCustomClass Function
回答by Mykola Golubyev
The one solution is to use built ctags database. So create one with the ctagsutility. Then set the 'tags' variable and put the following to the
一种解决方案是使用内置的 ctags 数据库。因此,使用ctags实用程序创建一个。然后设置“标签”变量并将以下内容放入
~/.vim/after/syntax/c.vim
function! s:highlight()
let list = taglist('.*')
for item in list
let kind = item.kind
if kind == 'f' || kind == 'c'
let name = item.name
exec 'syntax keyword Identifier '.name
endif
endfor
endfunction
call s:highlight()
I must warn you that this can work very slow on the very big ctags database.
我必须警告你,这在非常大的 ctags 数据库上可能工作得非常慢。
Also there is one solutionon the vim.org but I didn't try this one. Let me know if it works for you.
vim.org 上也有一个解决方案,但我没有尝试过这个。请让我知道这对你有没有用。
回答by Charles
EDIT: color_coded may be too heavy for you. try octol/vim-cpp-enhanced-highlight. It supports C++11/14 and integrates what @Eduardo answers.
编辑: color_coded 对你来说可能太重了。尝试octol/vim-cpp-enhanced-highlight。它支持 C++11/14 并集成了@Eduardo 的回答。
Semanticbased highlighter:
I would recommend jeaye/color_coded,
A vim plugin for libclang-based highlighting
So sorry that i'm new to stackoverflow which means I've not enough reputation to post images. Go see its effects if you wanna give it a shot. :)
基于语义的荧光笔:
我会推荐jeaye/color_coded,一个用于基于 libclang 的突出显示的 vim 插件
很抱歉,我是 stackoverflow 的新手,这意味着我没有足够的声誉来发布图像。如果你想试一试,就去看看它的效果。:)
Pros:
优点:
- Easy installation
- Semantic highlighting
- Clighter mentioned as above, need vim compiled with
python2.7
. However, color_coded is written in C++ and provides lua binding -> C++.
- 简易安装
- 语义高亮
- 上面提到的Clighter,需要用.vim编译
python2.7
。但是,color_coded 是用 C++ 编写的,并提供了 lua 绑定 -> C++。
Cons:
缺点:
- It delays unless you make some vim events to acitve it.
- Customization is bit harder; you need to edit syntax/color_coded.vim yourself. But customization has been placed on its roadmap.
- 除非你做一些 vim 事件来激活它,否则它会延迟。
- 定制有点难;你需要自己编辑syntax/color_coded.vim。但定制已被列入其路线图。
Although it's still under development, it's increasingly gaining attention.
虽然它仍在开发中,但它越来越受到关注。
回答by Charles
Sergey, changing the first line from
Sergey,将第一行从
syn match cCustomParen "(" contains=cParen,cCppParen
to
到
syn match cCustomParen "(" contains=cParen contains=cCppParen
seems to fix it for me.
似乎为我修好了。
回答by Yogesh Arora
Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646Its does all ctags highlighting very efficiently for you
尝试使用此插件http://www.vim.org/scripts/script.php?script_id=2646它可以为您非常有效地突出显示所有 ctags