Working with Vim highlight groups

Vim

Highlight groups are used in Vim to control how the user interface will look. For example, to change the colour of highlighted search items, you would add this line to your ~/.vimrc or colorscheme in ~/.vim/colors/.

1
highlight Search ctermfg=0 ctermbg=226

This would make highlighted search items show up with a yellow background and black text (when running in a terminal). This allows customisation of everything you see on screen. However, given the breadth of customisation possible it can be difficult to figure out which groups apply to the particular item you want to change.

Showing highlight groups

This function will show what groups are being applied. Add to your ~/.vimrc, place your cursor over the item in question, and press <leader>sp to output the groups.

1
2
3
4
5
6
7
nmap <leader>sp :call <SID>SynStack()<CR> function! <SID>SynStack() if !exists("*synstack") return endif echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")') endfunc

For example, placing my cursor over a comment in a Ruby file gives this output.

1
['rubyMultilineComment', 'rubyComment']

You can now change how Ruby comments look using these groups.

This should greatly help when modifying colorschemes.

Outputting all highlight groups

It can also be very useful to see how all highlight groups look.

Whilst browsing the highlight help page recently (:help highlight) I found that you can output all groups currently active using a script that comes with Vim. Running :so $VIMRUNTIME/syntax/hitest.vim will show something similar to below.

There are many more lines in the full output, but it should be clear that this is very useful when debugging colorschemes.

Vim colorschemes should now be easier to create and modify.