class & function names highlighting in Vim
Solution 1
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
Solution 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
$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):
And the obligatory screenshot:
Solution 3
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
Solution 4
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.
Solution 5
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.
ivanTheTerrible
Updated on July 05, 2022Comments
-
ivanTheTerrible almost 2 years
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.
-
ivanTheTerrible about 15 yearsI installed the Taglist plugin already, but isn't it for showing function/method outlines instead of highlighting/coloring the text?
-
dirkgently about 15 years@ivanTheTerrible: Please go through the FAQ(<vim-taglist.sourceforge.net/faq.html>) and the how to documentation.
-
dirkgently about 15 years@Mykola Golubyev: The FAQ mentions how to get there I believe.
-
Mykola Golubyev about 15 years@Dirkgently: a) link is broken. b) taglist doesn't highlight source code.
-
Mykola Golubyev about 15 yearsThe question is about highlighting function names in the code.
-
ivanTheTerrible almost 15 yearsSorry you are supposed to be the winner of this bounty. But StackOverflow some how auto-picked the highest point one instead. Any knowledge we can change that so you can earn 200 points (if you want). Thanks alot anyway.
-
idbrii almost 14 yearsYou don't need to copy c.vim. Just create a new one as
vimfiles/syntax/c.vim
with the 8 lines. -
xApple over 13 yearsVery nice solution. I have adopted it. Is there any one one could refine the pattern so that it only highlights the function names when they are declared and not every time they are called ?
-
Negative Zero over 11 yearsYou are awesome. Enough said. I am trying to switch from Textmate to Vim and came across the same problem. This is a big thumb up for the switch!
-
Adrian almost 11 yearsWhat's the
(Sorry for imageshack)
for? Why not just embed the image? -
mMontu about 10 years@Cogwheel: it works on Vim 7.4 for .cpp files. As it is, the curly braces of a
if
are highlighted as an error on C files. Removing the reference tocCppParen
, similar to user176963's answer, makes it works for .c files too -- although I don't understand exactly the reason. -
Ayberk Özgür almost 10 yearsYes it breaks the folding indeed. This is the correct answer.
-
rfornal about 9 yearsCan you include some code/documentation that shows why this would be the correct answer? A link can change over time which would then make this answer wrong.
-
Greenonline almost 9 yearsThe screen shot appears to have died. Can you embed the image?
-
Ragnar almost 9 yearsThis can be made even better using stackoverflow.com/a/1819151/2716069. Then, the
cCustomParen
isn't needed any more, and thecCustomFunc
becomes"\w\+\ze\s*("
(Code folding still works using this I believe.) -
Janosimas almost 9 yearsI'm not currently using Vi but I found this: vim.wikia.com/wiki/Syntax-based_folding