11

Occasionally I want to override the default syntax highlighting colours and styles with my own preferences.

I imagine the most appropriate way to do this would be to create my own colorscheme. However I have a couple of questions.

  1. If I want to set a highlight for a specific syntax group in a specific language, does this belong in my colorscheme, or would it be better to place it in after/syntax/[filetype].vim?

    highlight jsAssignExpIdent cterm=bold gui=bold
    

    It seems a little odd to place obscure language-specific rules in the colorscheme, as they will be loaded whatever language I am working on, but it seems even worse to place highlight rules in the syntax file.

  2. Sometimes I create new syntax rules for a specific language, in after/syntax/[filetype].vim. In case other users want to employ these extensions, would it be appropriate for me to provide default highlight rules there which link to common default highlight groups? If another user wants to override that highlight colour, how should they do that?

    ::::: after/syntax/asm.vim :::::
    
    syn match asmHexNumber /\(0x\|\$\)[0-9A-Fa-f]\+/
    highlight default link asmHexNumber Number
    
9

First question

Highlight definitions belong to your colorscheme. The fact that they are loaded for every buffer, no matter what their language, shouldn't be a problem at all.

If you don't want to edit your colorscheme, you can put those highlight definitions in plugin/myhighlights.vim:

function! MyHighlights()
    highlight ...
    highlight ...
endfunction

augroup MyHighlights
    autocmd!
    autocmd ColorScheme * call MyHighlights()
augroup END

Second question

Your sample is exactly how you should do and how every syntax script does. This method lets the plugin developer define sane default without forcing specific colors down their user's throat.

  • Thanks for the confirmation. But I am concerned if I put highlight link ... commands in a syntax file that these will overwrite settings from the colorscheme each time a file with that filetype is loaded. Should I perhaps only do highlight link ... after checking that no highlight rule exists for that name? – joeytwiddle Jan 9 '16 at 5:53
  • Ah, I need not worry. Vim docs say: If there are already highlight settings for the {from-group}, the link is not made, unless the '!' is given. For a ":highlight link" command in a sourced file, you don't get an error message. This can be used to skip links for groups that already have settings. – joeytwiddle Jan 9 '16 at 6:18
  • But for people tweaking their colorschemes, a highlight link in the colorscheme could be overwritten later by a highlight link in a syntax file. To avoid this, actual arguments (ctermbg, guifg, ...) can be used instead of a link. – joeytwiddle Jan 9 '16 at 6:23
  • The user should have the last word on what colors are used for a given highlight group. To that effect, it is not uncommon to see language-specific highlight definitions in colorschemes. You should only use highlight link ... in your syntax script and let your users override it if they want. – romainl Jan 9 '16 at 9:51
  • My concern was that if users use highlight link ... in their colorscheme, then the syntax file might reset it later with its own highlight link .... I see now that good syntax files avoid this by using highlight default link .... That should probably be a correction to the asmHexNumber rule in my question. – joeytwiddle Jan 11 '16 at 5:19

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.