+4  A: 

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

dirkgently
I installed the Taglist plugin already, but isn't it for showing function/method outlines instead of highlighting/coloring the text?
ivanTheTerrible
@ivanTheTerrible: Please go through the FAQ(<http://vim-taglist.sourceforge.net/faq.html>) and the how to documentation.
dirkgently
I can't understand too how taglist can highlight your code.
Mykola Golubyev
@Mykola Golubyev: The FAQ mentions how to get there I believe.
dirkgently
@Dirkgently: a) link is broken. b) taglist doesn't highlight source code.
Mykola Golubyev
+5  A: 

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

thomasrutter
The question is about highlighting function names in the code.
Mykola Golubyev
+4  A: 

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.

Mykola Golubyev
i tried it...and as u said its very slow
Yogesh Arora
+14  A: 

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

http://img16.imageshack.us/img16/2073/vimy.png (Sorry for imageshack)

Eduardo
Sorry 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.
ivanTheTerrible
You don't need to copy c.vim. Just create a new one as `vimfiles/syntax/c.vim` with the 8 lines.
pydave
A: 

Eduardo, your options is nice (class and function hi), but it's also highlight last bracket ")", if code construction not in one line, like this:

        Console.WriteLine("object smStream is = {0}, sstrMemW is = {1}"
                , smStream == null ? true : false
                , sstrMemW == null ? true : false

                ); // here is wrong highlight

what you think about it? i'm not sportsmen to correct regex expression :) , could you help ?

+1  A: 

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.

some guy
A: 

Hi all,

I have posted a similar question on #defines here- http://stackoverflow.com/questions/1512602/highlighting-defined-value-in-vim

Your pointers/ suggestions are appreciated.

I'm hoping there is a way to highlight this like the cool answer suggested by Eduardo above.

A: 

Try using this plugin http://www.vim.org/scripts/script.php?script_id=2646 Its does all ctags highlighting very efficiently for you

Yogesh Arora