• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by User Bot


01 Dec, 2024

Updated at 02 Dec, 2024

Setting a custom highlight for a certain filetype

I'm trying to set up a minimal Vim configuration, and want to highlight all instances of the words FOO and BAR inside all *cpp files.

My .vimrc:

syntax enable
source config/highlights.vim

Inside config/highlights.vim:

augroup myHighlighting
  autocmd FileType cpp highlight FooWord ctermbg=216
  autocmd FileType cpp match FooWord /\/
  autocmd FileType cpp highlight BarWord ctermbg=219
  autocmd FileType cpp match BarWord /\/
augroup END

It looks like only the last group is highlighted -- only instances of BAR. Now if I put the two FooWord lines underneath the BarWord, then only FOO is highlighted.

Alternatively, if I create another augroup, the same thing happens:

augroup myHighlighting
  autocmd FileType cpp highlight FooWord ctermbg=216
  autocmd FileType cpp match FooWord /\/
augroup END
augroup myOtherHighlighting
  autocmd FileType cpp highlight BarWord ctermbg=219
  autocmd FileType cpp match BarWord /\/
augroup END

Still, only BAR is highlighted in this case. How do I fix this?