1

I'm trying to configure the highlight to highlight keywords of my C++ project.

Here is the content of ~/.vim/syntax/cpp.vim:

syn match    cCustomParen    "?=(" contains=cParen,cCppParen
syn match    cCustomFunc     "\w\+\s*(\@=" contains=cCustomParen

hi CustomFunc guifg=NONE guibg=NONE guisp=NONE gui=bold ctermfg=lightyellow ctermbg=NONE cterm=bold    
hi def link cCustomFunc CustomFunc

syn match cCustomClassName "(?<=^class\s)\w\+"
hi def link cCustomClassName CustomFunc

In a word, here I'm trying to highlight the function name and the class name. However, the function name is highlighted but the class name isn't.

enter image description here

As you see, the function names, such as func, size, have been highlighted by the color lightyellow, but the class names, such as Test, Test2 are still white.

How could I highlight the class name? My cCustomCLassName is not correct?

0

(?<=pattern) is PCRE syntax.

In Vim, you need to use :help /\@<=

Try this:

syn match cCustomClassName "\(^class\s\)\@<=\w\+"

N.B. Your cCustomParen and cCustomFunc don't look right, either, but I don't know Perl syntax very well, so I'm not sure what you're trying to do there.

As a side note, the way you're including this file in your config also doesn't look quite right. If you want to add your highlighting in addition to the existing highlighting included in Vim, you should put it in the location ~/.vim/after/syntax/cpp.vim

If you want to replace Vim's standard highlighting, then you need to set b:current_syntax in your file in order to prevent Vim from loading its own file (which could overwrite some of your highlighting):

let b:current_syntax = 'cpp'

See :help mysyntaxfile-add and :help mysyntaxfile-replace for more details.

  • Note that it will only only highlight the class declaration/definition, not its uses. – Luc Hermitte Sep 20 at 12:47
0

One "simple" (read "low-tech") solution is to analyse your project with (universal-)ctags, extract only the class names and assign them a highlight group in all buffers. Unfortunately, this is not that simple to do that efficiently because:

  • a new class has to be propagated to all buffers already opened, and all buffers yet to be opened (when they are opened)
  • it'll be best to not add again the class names already highlighted

In my lh-tags plugin I was planning to have such an option, but given the complexity to do it right, the low need I have of the feature I've just opened an issue for the day I have the time and the motivation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.