1

I want to enable syntax highlighting for custom types in C files. So, in order to highlight a type name MyCoolType, I add the following line to ~/.vim/after/syntax/c.vim:

syn keyword Type MyCoolType

This works as I expect it. The keyword is highlighted with the color of the Type group.

Now I want to highlight custom types on a per project basis. To do so, I can manually issue :syn keyword Type MyCoolType, which also highlights the keyword in a currently loaded buffer. This is, however, tedious.

When I add the command to my project-local .vimrc, highlighting does not work. I also tried the following autocommands in my project-local .vimrc, which both did not work.

au BufRead *.c syn keyword Type MyCoolType
au FileType c syn keyword Type MyCoolType

Where and how do I define project local keyword highlighting in Vim 8.2?

3
  • 1
    Using au FileType c works for me, but maybe you have a different C syntax file? Try using au Syntax c so it gets triggered after the syntax option is set (which happens after filetype). Jun 27 at 9:54
  • 1
    Is the first line of code supposed to be syn[tax] keyword …?
    – D. Ben Knoble
    Jun 27 at 12:18
  • There was some strange interference with vim-polyglot. The autocommand started working as soon as vim-polyglot was deactivated. After de- and reinstall, the autocommand works now with vim-polyglot activated. I have no clue about the root of the problem.
    – MaxPowers
    Jun 28 at 8:42

Your Answer

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

Browse other questions tagged or ask your own question.