sponsor Vim development Vim logo Vim Book Ad

basic Tip #641: Highlighting of method names in the definition (C++)

 tip karma   Rating 18/9, Viewed by 884 

created:   January 21, 2004 10:53      complexity:   basic
author:   Olivier Teuliere      as of Vim:   6.0

When editing big cpp files, it can be very convenient to highlight the method name
(the part after "::") in a method definition.

--> I use the following function in my .vimrc:

" Add highlighting for function definition in C++
function! EnhanceCppSyntax()
    syn match cppFuncDef "::\~\?\zs\h\w*\ze([^)]*\()\s*\(const\)\?\)\?$"
    hi def link cppFuncDef Special
endfunction

--> I have another line to call this function automatically when editing a C++ file:

autocmd Syntax cpp call EnhanceCppSyntax()

--> That's it! This doesn't work in all cases (for instance, it doesn't highlight constructors
using an initialization list on the same line) but it shouldn't highlight function calls
(such as "MyClass::MyStaticMethod( int foo );" )
Don't hesitate to extend the regular expression for a more accurate matching...

 rate this tip  Life Changing Helpful Unfulfilling 

<<See your vim templates in Windows Explorer's New context menu | Windows: Get K to not display a DOS box that needs closing >>

Additional Notes

Niklas, January 22, 2004 16:41
Hi,

good tip! To further facilitate adding user-defined extensions to existing syntax files, you can use a ".vim/after/syntax/" directory. See:

    :help mysyntaxfile-add
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo