Search code examples
vivimvim-syntax-highlighting

Vim syntax highlighting for special comments


Is there a simple way in Vim to extend the syntax highlighting for a language to allow for important comments to stand out? For instance, if a line that starts with // denotes a regular comment in a C file, I'd like to have lines starting with //!! be highlighted in a more prominent color.

// this is a regular comment - line color should be the default color for comments

//!! this is an important comment - highlight line in red

Solution

  • :syn match specialComment #//!!.*# | hi specialComment ctermfg=red guifg=red
    

    As Ingo Karkat points out, you can have the commands executing after a .c file is loaded by placing them in ~/.vim/after/syntax/c.vim.

    Another option, if you'd like to place everything in a single file, such as ~/.vimrc, could be to bind the commands to the buffer enter event:

    au! BufEnter *.c syn match specialComment #//!!.*#  " C files (*.c)
    au! BufEnter *.py syn match specialComment /#!!.*/  " Python files (*.py)
    
    ...
    
    hi specialComment ctermfg=red guifg=red