2

I have tried to extend the C syntax in Vim with the example in the manual.

First I launch vim without any config:

vim -u NONE test.c

Then I load the c-syntax and create my extension

syntax on
syntax keyword myword HELP containedin=cComment contained
highlight myword guibg=red

code:

1: /* HELP */
2: int main(int argc, char **argv) {
3:     int HELP = 0;
4:     if(HELP);
5: }

HELP is highlighted on line 1 as expected. It is also highlighted on line 4, is there some explanation for this?

0

You need to find out which syntax group "occupies" the HELP in the third line that is not covered by your syntax rule. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.

SyntaxAttr tells me the cBlock syntax matches there; this needs to be included in your extension, too:

syntax keyword myword HELP containedin=cComment,cBlock contained
  • If I :call SyntaxAttr() when the cursor is within the parentheses, it only gives me group: myword guibg=red(#ff0000). I do not get why I should include cBlock, since I want to restrict the number of places where the myword highlighting is used. – Klas. S Nov 2 '17 at 23:15
  • The cBlock encompasses everything within {...} and prevents the match. This definition is found in $VIMRUNTIME/syntax/c.vim. It would obscure the HELP in line 4, too; however, that is contained in cParen which contains=ALLBUT,... The ALL makes this include your myword, and so it is highlighted without you having to containedin=cParen. – Ingo Karkat Nov 3 '17 at 8:08

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.