Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

class & function names highlighting in Vim

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.

like image 792
ivanTheTerrible Avatar asked Apr 10 '09 04:04

ivanTheTerrible


People also ask

Is class owned by Zoom?

Zoom, the popular video conferencing tool, has been adapted by a startup -- ClassEDU -- founded by education technology veterans including the Blackboard co-founder and former CEO.

What is Classroom learning?

Classroom learning: Classroom learning is a traditional mode of learning in which the learning environment is created within the physical walls of a classroom. As the name suggests, in-classroom learning, both the teacher and student need to be present physically inside the classroom.

What does class setting mean?

Related Definitions Classroom setting means a setting, conducive to learning and free from distractions, for which the primary purpose is education, instruction, training, or conference. Participants must be able to simultaneously interact with each other as well as with the instructor.

Who uses ClassIn?

Currently, ClassIn has over 60,000 partner schools and institutions and 20 million monthly active users from more than 150 countries like the UK, US, Italy, Brazil, Japan, Argentina, UAE, Spain, and now, the Philippines, successfully delivering 200,000,000+ class hours.


2 Answers

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

like image 159
Eduardo Avatar answered Sep 21 '22 04:09

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.

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 
like image 38
Janosimas Avatar answered Sep 17 '22 04:09

Janosimas