1

I'm trying to use the vim-syntax to configure some custom highlight for my c++ project.

I've created a file named cpp.vim and put it into ~/.vim/syntax/. And to highlight the function name, I put

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

into the file cpp.vim, it works as expected.

Now I'm trying to highlight the class name.

Here is what I've tried:

hi CustomClassName guifg=NONE guibg=NONE guisp=NONE gui=bold ctermfg=lightyellow ctermbg=NONE cterm=bold
syn match cCustomClassName "(?<=^class\s)\w\+"
hi def link cCustomClassName CustomClassName

However, the class name is still white.

(?<=^class\s)\w\+ is trying to match a word, which is following a class and a space. For example, this regex can match things like class Test ignoring class, which is exactly what regular expression lookbehind should do.

I've tested this regex with some online tool and it works perfectly. So I don't know why it can not work for the vim syntax config.

Yves
  • 11,597
  • 17
  • 83
  • 180
  • 1
    Lookarounds in vim are different in syntax. You should try `\(REGEX\)\@<=` for a positive lookbehind. – revo Sep 20 '19 at 06:31
  • @revo it works! thanks a lot. Pose an answer maybe? – Yves Sep 20 '19 at 07:23
  • I'm glad that it worked. I found a question that points to the same problem and marked this as a duplicate. – revo Sep 20 '19 at 07:45

0 Answers0