a1pancakes
a1pancakes

Reputation: 513

vim own highlight with specific character

I want my vim to highlight in red some keywords from the Pouet group like 'if(' in my .c files. I figured out how to highlight if with:

syn keyword Pouet if

(This is my ~/.vim/syntax/c.vim)

and with

highlight Pouet term=NONE cterm=NONE Ctermfg=160 ctermbg=NONE gui=NONE

(And this is a part of my .vimrc)

The problem is,this code doesn't work with special characters like '(' or maybe a space or many spaces. My question is: how do I make sentences like 'if(' highlight in red ?

Thanks

Upvotes: 6

Views: 2462

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172510

:syn keyword only works for keyword characters (as defined by the 'iskeyword' setting), and ( usually is not contained.

You have to use :syn match instead, e.g.:

:syn match Pouet "\<if("

This is fine if you define your syntax all on your own. If you want this in addition to the existing C syntax highlighting, you need to analyze the original syntax groups and add stuff like containedin=cConditional, maybe you even have to modify the original syntax definition.

An alternative is matchadd(), which goes on top of the syntax highlighting:

:call matchadd('Pouet', '\<if(')

The problem here is that these matches are window-local, not bound to the filetype like syntax highlighting, so when you split windows or edit another filetype in the current window, the highlighting will be gone / will persist. These problems can be worked around with autocmds, but now it's getting really complex.

Upvotes: 4

Related Questions