类 & Vim 中函数名称高亮显示
在沉迷于它的模态输入之后,我最近刚刚从 Textmate 设置了我的 Vim 环境。
不过,Vim 中的语法高亮似乎不太美观。 我用 C++ 编写代码,由于函数调用和类名无法突出显示,因此代码更难以阅读。 我玩了一下配色方案,但找不到任何与“类名”或“函数名”相对应的字段。
在下图中,请注意 DroughtLayer::
和 *.size()
在 MacVim 中的右侧没有突出显示。
(来源:ivzhao.com)
有什么想法如何解决这个问题吗? 这真的让我很恼火,因为我是一个视觉敏感的人。
I just recently set up my Vim environment from Textmate, after becoming addicted to its modal input.
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".
In the picture below, notice how DroughtLayer::
and *.size()
is not highlighted on the right in MacVim.
(source: ivzhao.com)
Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
当我开始使用 vim 时,我也遇到了同样的问题。 解决方案很简单,你只需编辑 vim 使用的 c 语法文件,具体方法如下:
当你开始编辑 C 或 C++ 文件时,vim 会读取位于
(其中 $VIMRUNTIME 是你的位置) 的 默认 c 语法文件已经安装了 vim。你可以通过打开 vim 并使用命令“:echo $VIMRUNTIME”来找到它的默认值。
你可以简单地覆盖该文件,或者你可以在这个位置创建你的自定义 C 语法文件(它将由 vim 加载,而不是默认的):(
我从未使用过 Mac,所以我不知道哪一个可以工作你可以在 vim 帮助中找到更多信息,“:help vimfiles”)
现在是有趣的部分。 将默认的“$VIMRUNTIME/syntax/c.vim”文件复制到 vimfiles 目录(对于 UNIX 为“$HOME/.vim/syntax/c.vim”),并通过添加以下行来编辑它:
就是这样! 现在,函数和类名将使用“函数”突出显示中定义的颜色突出显示(“:hi Function”)。 如果你想自定义颜色,你可以将上面的最后两行更改为如下所示:
或者你可以保留 C 语法文件并在 vimrc 文件中定义颜色(“:help vimrc”):(
注意缺少“def”关键字,请参阅“:helphighlight-default”了解详细信息)。 有关“:hi”命令的可用参数,请参阅“:help :highlight”。
您可以在此链接上找到 Vim 7.2 的完整 c.vim 文件(注意:仅当您有未修改的 Vim 7.2 版时才使用此文件):
以及必要的屏幕截图:
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:
When you start editing a C or C++ file, vim reads the default c syntax file located in
(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").
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:
(I have never used a Mac so I don't know which one will work for you. You can find out more in the vim help, ":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:
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:
or you can leave the C syntax file alone and define colors in your vimrc file (":help vimrc"):
(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".
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):
And the obligatory screenshot:
这是我在这里的第一篇文章,我不知道如何进行观察,Eduardo 的答案使“(”和“{”看起来不匹配并且错误语法折叠,我对其进行了一些更改以解决此问题。
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.
有趣的是,VIM 中的语法突出显示不支持将语法应用于标识符或函数名称 - 至少 C 和 C++ 的语法突出显示不支持。 所以,即使你这样做:
或者
它不会给这些颜色。 我对这些语言来说似乎只不过是关键字和常量而已。
在这里,有人开始扩展 cpp 语法文件以支持方法名称。 我想这是一个开始。
http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition
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:
or
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
一种解决方案是使用内置的 ctags 数据库。 因此,使用 ctags 实用程序创建一个。 然后设置“tags”变量并将以下内容放入
我必须警告您,这在非常大的 ctags 数据库上运行速度会非常慢。
vim.org 上还有一个解决方案,但我没有试试这个。 请让我知道这对你有没有用。
The one solution is to use built ctags database. So create one with the ctags utility. Then set the 'tags' variable and put the following to the
I must warn you that this can work very slow on the very big ctags database.
Also there is one solution on the vim.org but I didn't try this one. Let me know if it works for you.
编辑: color_coded 对你来说可能太重了。 尝试 octol/vim-cpp-enhanced-highlight。 它支持 C++11/14 并集成了 @Eduardo 的答案。
基于语义的荧光笔:
我会推荐 jeaye/color_coded,
一个基于 libclang 突出显示的 vim 插件
很抱歉,我是 stackoverflow 的新手,这意味着我没有足够的声誉来发布图像。 如果你想尝试的话就去看看它的效果吧。 :)
优点:
python2.7
编译 vim。然而color_coded是用C++编写的,并提供lua绑定->
C++。
缺点:
你自己。 但定制已被列入其路线图。
尽管它仍在开发中,但它越来越受到关注。
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.
Semantic based 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. :)
Pros:
python2.7
.However, color_coded is written in C++ and provides lua binding ->
C++.
Cons:
yourself. But customization has been placed on its roadmap.
Although it's still under development, it's increasingly gaining attention.
谢尔盖,将第一行从 更改
为
似乎可以为我解决问题。
Sergey, changing the first line from
to
seems to fix it for me.
尝试使用这个插件 http://www.vim.org/scripts/script。 php?script_id=2646
它可以非常有效地为您突出显示所有 ctags
Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646
Its does all ctags highlighting very efficiently for you
使用 vim 插件,例如 Taglist 或设置
ctags
或cscope
与 vim 集成(这里是 vim/cscope 的教程.)Use a plug-in for vim like Taglist or set up
ctags
orcscope
integration with vim (here's a tutorial for the vim/cscope.)我强烈推荐您使用
taghighlight
插件,请点击此处因为它的网站。I really recommend you the
taghighlight
plugin, click here for it's website.也可以考虑
Clighter
插件,这是一个但是,需要相当新的版本和软件:
vim 7.4.330 +python2
和libclang
。The
Clighter
plugin can also be considered, which is aHowever, requires fairly recent versions and software:
vim 7.4.330 +python2
andlibclang
.仅匹配 C 函数定义,这对我有用:
To match C functions definitions only, this works for me: