3

I am using VOoM (it is probably not needed to know much about it to be able to answer my question, I'm just providing this info for context) and I have managed to write myself a couple of custom syntax highlight rules for specific content.

First, I am using a modeline to tell vim that a certain file is a VOoM outline, I have this at the bottom of such files:

/* vim: set syntax=myvoom : */

Second, I have a /home/user/.vim/after/syntax/myvoom.vim file, here's a snippet from it:

syn match voomTag "^tags:.*"
hi voomTag ctermfg=White ctermbg=Red guifg=White guibg=Red

In some outline nodes I have lines like:

tags: foo, bar, baz

to make the searching easier, the syntax highlight rule above just displays those lines with a custom foreground/background.

This works as expected with default colorscheme (:colorscheme in vim returns 'default').

When I want to try a custom colorscheme (mustang for example) and change it by doing :colorscheme mustang, the syntax highlight for my tag lines is lost.

I would like to understand why this happens.

Also, I would like to learn how to auto-sync these custom syntax rules (which are general, not specific to a certain colorscheme) when the colorscheme is changed. Do I have to check for this explicitly somehow, in myvoom.vim file? Like looking what colorscheme is active and have custom syntax highlight rules for different colorschemes or is there a better approach?

1 Answer 1

5

The reason why this happens is in :highlight clear (see :help :highlight) command, which is normally one of first commands executed by colorscheme. This command resets all highlighting, thus removing effect of your custom :highlight command.

To fix this you need to use one of :autocmd (see help :autocmd) events, ColorScheme (see :help ColorScheme), to execute your command after new colorscheme is loaded. After changes your myvoom.vim should look like this:

augroup VoomTagHighlight
  autocmd! ColorScheme *
         \ syn match voomTag "^tags:.*" |
         \ hi voomTag ctermfg=White ctermbg=Red guifg=White guibg=Red
augroup END

See also help on :augroup at :help :augroup.

If you want to have different highlight depending on current colorscheme, use :highlight link command (see :help :highlight-link) instead of specifying your own colors. For example linking it to Error highlight group would look like this:

augroup VoomTagHighlight
  autocmd! ColorScheme *
         \ syn match voomTag "^tags:.*" |
         \ hi! link voomTag Error
augroup END

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .