I'm on Ubuntu 18.04, vim-gtk3 8.0.

I'm trying to get gvim to highlight particular words within a comment. For example, take the word 'NOTE'. In a '.c' file: // NOTE(Ryan): ... and a '.sh' file: # NOTE(Ryan): ... both instances of 'NOTE' would be highlighted. Using Highlight whole todo comment line as a guide, my '.gvimrc' is as follows:

augroup CommentMarkers
  autocmd Syntax * syntax match NoteMarker /\v\_.<NOTE/hs=s+1 containedin=.*Comment,vimCommentTitle
  autocmd Syntax * syntax match TodoMarker /\v\_.<TODO/hs=s+1 containedin=.*Comment,vimCommentTitle
  " etc...
augroup END

highlight NoteHighlight guibg=Green 
highlight! link NoteMarker NoteHighlight
highlight TodoHighlight guibg=Orange 
highlight! link TodoMarker TodoHighlight
" etc...

This works as expected when inside my '.gvimrc' file and a .sh file. Inside '.c' files however, it does not work. For 'TODO' it gives the default yellow background and 'NOTE' is blank. It seems that '.c' files are ignoring the above commands.

For more info, here is my entire .gvimrc file

Any suggestions would be greatly appreciated.



1 Answer 1


The reason that your code isn't working is that Vim's default syntax highlighting has two comment groups in C files. The first, for /* */ multiline comments, is named cComment, and you should find your highlights work fine in this style of comment.

The second is for // and it is named cCommentL. This doesn't match the .*Comment wildcard that I suggested in my other answer, so your syntax items are disallowed within. Tacking on ,cCommentL to the end of each line is a simple way to fix this.

There are a few other issues that I'd also like to address:

  1. All the regular expression shenanigans with \_. and hs were required in the other question in order to override a syntax keyword with a syntax match. You don't need to do this: simple syntax keyword items would suffice:

    autocmd Syntax * syntax keyword NoteMarker NOTE containedin=.*Comment,vimCommentTitle,cCommentL
    autocmd Syntax * syntax keyword TodoMarker TODO containedin=.*Comment,vimCommentTitle,cCommentL
  2. You actually don't need to set up a syntax item at all for TODO: you can just apply your highlighting to the existing items:

    highlight vimTodo guibg=Green
    highlight shTodo guibg=Green
    highlight cTodo guibg=Green
  3. I'm not sure if the code you pasted is actually a simplified minimum reproducible example, but as it stands, the NoteHighlight and TodoHighlight groups that you link to are unnecessary. You can just highlight directly:

    highlight NoteMarker guibg=Green
  4. Maybe you will want to keep them separate for future flexibility, but if you're highlighting them the same you don't need to use multiple syntax groups for this. You can have multiple keywords (or indeed matches) in a single syntax group:

    autocmd Syntax * syntax keyword NoteMarker NOTE TODO containedin=.*Comment,vimCommentTitle,cCommentL
  • Thank you for your suggestion and then some! I started out with the basic syntax keyword invocation however it was wasn't working, so I went for the long-wounded regex hoping it would solve it. Little did I know the problem was the 'cCommentL'.... Commented Jun 17, 2020 at 3:48
  • Say I wanted to have the highlighting extend an entire comment region, i.e. over multiple lines, is this possible and if so, how would this be done? Commented Jun 17, 2020 at 4:36
  • @RyanMcClue You mean you want your NOTE highlighting to extend from the NOTE marker to the end of the comment? I think it's possible, but it doesn't look like there's a simple way to do it that I can fit in a comment. Have a read of (at least) :h 27.8, :h :syn-region, :h :syn-keepend and see what you can come up with. You'll probably need to take a look at the definitions of the existing comment syntax items in e.g. :e $VIMRUNTIME/syntax/c.vim, too. If you can't figure it out, it's probably worth asking another question once you've had a go.
    – Rich
    Commented Jun 17, 2020 at 8:52
  • @RyanMcClue One tip that's very useful when fiddling with syntax items, is the Example at the bottom of :h synID() which explains how to query the syntax item under the cursor. I've extended this a little into a little custom command in my vimrc which displays the containing items and highlighting, too.
    – Rich
    Commented Jun 17, 2020 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.