I'm creating a colorscheme for vim and want to give the ifand endif distinct colors.

I followed the tutorial on vimcasts and have the following file

"set 256 colors for gnome-terminal
if $COLORTERM == 'gnome-terminal'
    set t_Co=256
set background=dark
highlight clear
if exists("syntax_on")
    syntax reset
let g:colors_name = "my_color_scheme"

highlight guibg=#110117 ctermbg=233
highlight vimcommand guifg=#6792db ctermfg=68 guibg=NONE
highlight link number vimstring
highlight vimnotfunc guifg=#e6b13e ctermfg=179 guibg=NONE

The last lines changes the color of if but not of endif.

I used the snippet to find the name of the syntax

" Show syntax highlighting groups for word under cursor
nmap <C-S-P> :call <SID>SynStack()<CR>
function! <SID>SynStack()
  if !exists("*synstack")
  echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')

It shows ['vimIsCommand', 'vimCommand']. Setting an fg color for any of them does not seem to change the highlighting.

How can I find the right group name?


First, a colorscheme just provides a mapping of (mostly generic) highlight groups to certain colors and text attributes. A syntax script provides parsing rules to identify syntax elements. Those syntax groups (e.g. vimCommand) are then linked to the generic highlight groups (such as Statement).

Though a custom colorscheme could indeed provide a special color for, say, vimCommand, this is unusual. You can do that in your own colorscheme, but you can also perform the :hi link ... override in your ~/.vimrc.

In any case, you can only assign distinct colors to separate syntax groups.

The SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin is similar to the snippet in your question. With it, and by looking into the syntax definition in $VIMRUNTIME/syntax/vim.vim, I see that if belongs to the vimNotFunc group (together with elseif, return, and while), whereas endif is put together with all Vimscript commands into vimCommand. So, any color change would also affect many other commands, which may not be what you want.

To fix that, you'd need to tweak the syntax rules itself, by defining overriding :syntax commands, and putting them in ~/.vim/after/syntax/vim.vim. That's possible, but tedious (especially considering you might want to have the same for other languages, too). I'd rather reconsider whether you don't live with the predefined groupings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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