类 & Vim 中函数名称高亮显示

发布于 2024-07-17 19:25:22 字数 544 浏览 18 评论 0原文

在沉迷于它的模态输入之后,我最近刚刚从 Textmate 设置了我的 Vim 环境。

不过,Vim 中的语法高亮似乎不太美观。 我用 C++ 编写代码,由于函数调用和类名无法突出显示,因此代码更难以阅读。 我玩了一下配色方案,但找不到任何与“类名”或“函数名”相对应的字段。

在下图中,请注意 DroughtLayer::*.size() 在 MacVim 中的右侧没有突出显示。

图片对比Textmate(左)和 Vim(右)
(来源: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.

Picture comparison between Textmate(left) and Vim(right)
(source: ivzhao.com)

Any ideas how to solve this? It really annoys me as I am so much a visual-sensitive guy.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

南城旧梦 2024-07-24 19:25:22

当我开始使用 vim 时,我也遇到了同样的问题。 解决方案很简单,你只需编辑 vim 使用的 c 语法文件,具体方法如下:

当你开始编辑 C 或 C++ 文件时,vim 会读取位于

$VIMRUNTIME/syntax/c.vim

(其中 $VIMRUNTIME 是你的位置) 的 默认 c 语法文件已经安装了 vim。你可以通过打开 vim 并使用命令“:echo $VIMRUNTIME”来找到它的默认值。

你可以简单地覆盖该文件,或者你可以在这个位置创建你的自定义 C 语法文件(它将由 vim 加载,而不是默认的):(

$HOME/.vim/syntax/c.vim      (for UNIX)
$HOME/vimfiles/syntax/c.vim  (for PC or OS/2)

我从未使用过 Mac,所以我不知道哪一个可以工作你可以在 vim 帮助中找到更多信息,“:help vimfiles”)

现在是有趣的部分。 将默认的“$VIMRUNTIME/syntax/c.vim”文件复制到 vimfiles 目录(对于 UNIX 为“$HOME/.vim/syntax/c.vim”),并通过添加以下行来编辑它:

" 突出显示类和函数名称 
  syn 匹配 cCustomParen "(" contains=cParen,cCppParen 
  syn 匹配 cCustomFunc "\w\+\s*(" contains=cCustomParen 
  syn 匹配 cCustomScope "::" 
  syn 匹配 cCustomClass "\w\+\s*::" contains=cCustomScope 

  hi def 链接 cCustomFunc 函数 
  hi def 链接 cCustomClass 函数 
  

就是这样! 现在,函数和类名将使用“函数”突出显示中定义的颜色突出显示(“:hi Function”)。 如果你想自定义颜色,你可以将上面的最后两行更改为如下所示:

hi def cCustomFunc  gui=bold guifg=yellowgreen
hi def cCustomClass gui=reverse guifg=#00FF00

或者你可以保留 C 语法文件并在 vimrc 文件中定义颜色(“:help vimrc”):(

hi cCustomFunc  gui=bold guifg=yellowgreen
hi cCustomClass gui=reverse guifg=#00FF00

注意缺少“def”关键字,请参阅“:helphighlight-default”了解详细信息)。 有关“:hi”命令的可用参数,请参阅“:help :highlight”。

您可以在此链接上找到 Vim 7.2 的完整 c.vim 文件(注意:仅当您有未修改的 Vim 7.2 版时才使用此文件):

http://pastebin.com/f33aeab77

以及必要的屏幕截图:

在此输入图像描述

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

$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").

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:

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

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

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".

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

http://pastebin.com/f33aeab77

And the obligatory screenshot:

enter image description here

拒绝两难 2024-07-24 19:25:22

这是我在这里的第一篇文章,我不知道如何进行观察,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

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.

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
沧桑㈠ 2024-07-24 19:25:22

有趣的是,VIM 中的语法突出显示不支持将语法应用于标识符或函数名称 - 至少 C 和 C++ 的语法突出显示不支持。 所以,即使你这样做:

:hi Function guifg=red

或者

:hi Identifier guifg=red

它不会给这些颜色。 我对这些语言来说似乎只不过是关键字和常量而已。

在这里,有人开始扩展 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:

: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

不奢求什么 2024-07-24 19:25:22

一种解决方案是使用内置的 ctags 数据库。 因此,使用 ctags 实用程序创建一个。 然后设置“tags”变量并将以下内容放入

~/.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()

我必须警告您,这在非常大的 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

~/.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.

Also there is one solution on the vim.org but I didn't try this one. Let me know if it works for you.

往日情怀 2024-07-24 19:25:22

编辑: color_coded 对你来说可能太重了。 尝试 octol/vim-cpp-enhanced-highlight。 它支持 C++11/14 并集成了 @Eduardo 的答案。

基于语义的荧光笔:
我会推荐 jeaye/color_coded
一个基于 libclang 突出显示的 vim 插件
很抱歉,我是 stackoverflow 的新手,这意味着我没有足够的声誉来发布图像。 如果你想尝试的话就去看看它的效果吧。 :)

优点:

  • 安装方便
  • 语义高亮
  • Clighter 如上所述,需要使用 python2.7 编译 vim。
    然而color_coded是用C++编写的,并提供lua绑定->
    C++。

缺点:

  • 除非你创建一些 vim 事件来激活它,否则它会延迟。
  • 定制有点困难; 你需要编辑syntax/color_coded.vim
    你自己。 但定制已被列入其路线图。

尽管它仍在开发中,但它越来越受到关注。

之前
之后

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:

  • 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++.

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.

Although it's still under development, it's increasingly gaining attention.

before
after

染墨丶若流云 2024-07-24 19:25:22

谢尔盖,将第一行从 更改

syn match    cCustomParen    "(" contains=cParen,cCppParen

syn match    cCustomParen    "(" contains=cParen contains=cCppParen

似乎可以为我解决问题。

Sergey, changing the first line from

syn match    cCustomParen    "(" contains=cParen,cCppParen

to

syn match    cCustomParen    "(" contains=cParen contains=cCppParen

seems to fix it for me.

一梦等七年七年为一梦 2024-07-24 19:25:22

尝试使用这个插件 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

夏有森光若流苏 2024-07-24 19:25:22

使用 vim 插件,例如 Taglist 或设置 ctagscscope 与 vim 集成(这里是 vim/cscope 的教程.)

Use a plug-in for vim like Taglist or set up ctags or cscope integration with vim (here's a tutorial for the vim/cscope.)

清引 2024-07-24 19:25:22

我强烈推荐您使用 taghighlight 插件,请点击此处因为它的网站。

I really recommend you the taghighlight plugin, click here for it's website.

心奴独伤 2024-07-24 19:25:22

也可以考虑 Clighter 插件,这是一个

plugin for c-family semantic source code highlighting, based on Clang

但是,需要相当新的版本和软件:vim 7.4.330 +python2libclang

The Clighter plugin can also be considered, which is a

plugin for c-family semantic source code highlighting, based on Clang

However, requires fairly recent versions and software: vim 7.4.330 +python2 and libclang.

债姬 2024-07-24 19:25:22

仅匹配 C 函数定义,这对我有用:

syn match    cCustomFuncDef display /\(\w\+\(\s\|*\)\+\)\@<=\w\+\s*(\@=/ 
hi def cCustomFuncDef ctermfg=lightblue

To match C functions definitions only, this works for me:

syn match    cCustomFuncDef display /\(\w\+\(\s\|*\)\+\)\@<=\w\+\s*(\@=/ 
hi def cCustomFuncDef ctermfg=lightblue
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文