Class and function names in Vim

I just recently set up my Vim environment from Textmate after I became dependent on its modal input.

However, the syntax highlighting in Vim does not look very nice. I am coding in C ++, and since function calls and class names cannot be allocated, the code is harder to read. I played a little with the color scheme, but could not find any fields corresponding to the "class name" or "function name".

In the image below, notice that DroughtLayer:: and *.size() 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 since I'm a very sensitive guy.

+68
c ++ vim syntax-highlighting textmate vim-syntax-highlighting
Apr 10 '09 at 4:27
source share
10 answers

Interestingly, syntax highlighting in VIM does not support applying syntax to identifiers or function names β€” at least not syntax highlighting for C and C ++. So even if you do this:

 :hi Function guifg=red 

or

 :hi Identifier guifg=red 

he does not give color. It seems to me that these are nothing but keywords and constants for these languages.

Here, someone started extending the cpp syntax file to support method names. This is probably the beginning. http://vim.wikia.com/wiki/Highlighting_of_method_names_in_the_definition

+17
Apr 15 '09 at 15:49
source share

I had the same problem when I started using vim. The solution is simple, you just need 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 at

 $VIMRUNTIME/syntax/c.vim 

(Where $ VIMRUNTIME is where you installed vim. You can find out its default value by opening vim and using the command:: echo $ VIMRUNTIME).

You can simply overwrite this file or create your own C syntax file (which will be loaded by vim instead of the default file) in this place:

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

(I 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 file "$ VIMRUNTIME / syntax / c.vim" to the vimfiles directory ("$ HOME / .vim / syntax / c.vim" for UNIX) and edit it by adding the following 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 

It! Now the names of functions and classes will be highlighted with the color defined in the selection "Function" (": hi Function"). If you want to customize the 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 the colors in your vimrc file (": help vimrc"):

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

(Note the lack of the def keyword, go to ": help highlight-default" for more information). For available options for the ": hi" command, see ": help: highlight".

You can find the full c.vim file for Vim 7.2 at this link (Note: use it only if you have unmodified Vim version 7.2):

http://pastebin.com/f33aeab77

And a required screenshot:

enter image description here

+118
Apr 21 '09 at 16:09
source share

This is my first post here, and I did not know how to make an observation, Eduardo's answer makes "(" and "{" look unmanageable and syntactically syntactic, I changed it a bit 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 
+33
Apr 13 2018-12-12T00:
source share

The only solution is to use the built-in ctags database. Therefore, create it using the ctags utility. Then set the tag variable and put the following in

 ~/.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 slowly in a very large ctags database.

There is also one solution on vim.org, but I have not tried this. Let me know if this works for you.

+10
Apr 16 '09 at 10:43
source share

EDIT: color_coded may be too heavy for you. try octol / vim-cpp-extended-highlight . It supports C ++ 11/14 and integrates what @Eduardo answers.

Semantics- based highlighting:
I would recommend jeaye / color_coded , a vim plugin for libclang based highlighting
Sorry I'm new to stackoverflow, which means I don't have enough reputation to post images. Go look at its effects if you want to try. :)

Pros:

  • Easy installation
  • Semantic highlighting
  • Clighter mentioned above needs vim compiled with python2.7 . However, color_coded is written in C ++ and provides a lua β†’ C ++ binding.

Minuses:

  • This is delayed unless you make a few vim events to activate it.
  • The setup is a little trickier; you need to edit the syntax /color_coded.vim yourself. But the setting was posted on his roadmap.

Although it is still under development, it is increasingly attracting attention.

beforeafter

+6
Apr 03 '15 at
source share

Sergey, changing the first line of

 syn match cCustomParen "(" contains=cParen,cCppParen 

to

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

seems to fix this for me.

+3
Sep 22 '09 at 6:07
source share

Use a plugin for vim, like Taglist, or configure the integration of ctags or cscope with vim ( here is a tutorial for vim / cscope.)

+2
Apr 10 '09 at 4:39
source share

Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646 It is made by all the ctags that very efficiently give you

+2
Apr 29 '10 at 21:20
source share

I really recommend you the taghighlight plugin, click here for this website.

+1
Jul 6 '14 at 4:14
source share

You can also consider the Clighter plugin, which is

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

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

+1
Aug 13 '14 at 10:18
source share



All Articles