I'm using a syntax file in vim that defines a number of filetype-specific syntax keywords. To color files of this type I've also created a colorscheme file for the share directory that attempts to highlight these syntax keywords, however they don't take effect when I open files of that extension.
My color file does however highlight normal groups such as Normal
, Special
, Comment
, etc. As well, when I attempt to move these highlight commands to my .vimrc file, they still have no effect. However, after the file is loaded in vim, entering the highlight commands manually works as intended.
Is there something special I need to do in order to use syntax keywords defined in the syntax files? Do I maybe need to specify the load ordering of my syntax files & color files in my .vimrc?
EDIT: using :scriptnames
, I am able to see that my custom colorscheme file loads long before the syntax file, which in fact loads dead last. However, my .vimrc file specifies the colorscheme CustomPersonal
as the last line, far after syntax on
.
Vim has a built-in mechanism for overriding syntax groups, so if you do everything right (even in your ~/.vimrc
), this should work. The solution suggested by @MatthewStrawbridge might work, but feels wrong, because colorschemes are global in Vim, and therefore shouldn't be specified in an ftplugin
.
Here's how the overriding works:
Most filetypes (like python
) in Vim come with a syntax that defines highlight groups (see them via :highlight
). These particular groups (e.g. pythonFunction
) are then linked to a set of default groups (:help highlight-groups
, e.g. Identifier
). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic (separately for terminals, color terminals, and/or GVIM) for the default groups.
highlight group → default group → color + style
pythonFunction
→ Identifier
→ term=underline ctermfg=3 guifg=DarkCyan
So, for a set of beautifully matching colors that please your personal taste, you choose a colorscheme. In order to tweak some particular associations, you can change the linking of highlight group to default group, e.g.:
:hi link pythonFunction Special
This can already be done in ~/.vimrc
. As long as the syntax script correctly uses :hi def link
, the original link established by you will be kept; the def[ault]
parameter ensures that the link will only be created if no link exists yet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With