Search code examples
c++vimcommentsvim-syntax-highlighting

Custom Keyword highlighting within comments for VIM


I want to add custom keywords that should be highlighted in vim. These keywords are for marking comments, not the code. This is what I have in my .vimrc:

autocmd Syntax * syntax keyword Important containedin=.*Comment contained IMPORTANT

This works, but only partly, as the keyword (IMPORTANT) is only highlighted when using 'C-Style' comments: ( /* ... */ ), not the shorter( // ... ) notation. I've played around with the 'containedin=' flag to no success..

This needs to work for C/C++ but obviously would be nice to have in general!


Solution

  • You need to find out which syntax group causes the highlighting. :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.

    When I trigger that on a // comment line in a C++ file, it lists the syntax group as cCommentL. So, to fix this, you need to extend the regular expression, either strictly to containedin=.*CommentL\?, or more encompassing to containedin=.*Comment.*