class & function names highlighting in Vim

2019-01-07 02:17发布

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) http://ivzhao.com/temp/vimHL.png

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

10条回答
Fickle 薄情
2楼-- · 2019-01-07 02:49

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.

查看更多
Root(大扎)
3楼-- · 2019-01-07 02:50

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

查看更多
The star\"
4楼-- · 2019-01-07 02:52

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.

查看更多
祖国的老花朵
5楼-- · 2019-01-07 02:53

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

查看更多
登录 后发表回答