I've been editing my syntax files, placing my versions in ~/.vim/syntax/.

For ~/.vim/syntax/php.vim that's worked, but for ~/.vim/syntax/html.vim for some reason it's being overridden by /usr/share/nvim/runtime/syntax/html.vim.

Is there something I can put in my .vimrc file to make this choice explicit? Or some other way?

(FWIW my init.vim declares set runtimepath^=~/.vim runtimepath+=~/.vim/after)

  • Is there any good reason to overwrite syntax fully rather than extend it? In any case, Vim always loads all sytax files matching "xyz.vim" found in runtimepath.
    – Matt
    Jan 14 at 18:24
  • I guess simplicity - very easy to copy a file and add to/edit it. I didn't know they could be extended and wouldn't obviously know how to. Happy to learn though.
    – Codemonkey
    Jan 14 at 20:00
  • :h after-directory
    – Matt
    Jan 14 at 20:03

Vim (or NeoVim) will always load all syntax files it finds for a specific filetype.

To allow you to override the syntax configuration bundled in Vim runtime, those scripts typically include a check at the top that will skip the execution of the rest of the script if a variable is already set. For syntax files, that is typically the buffer-local variable b:current_syntax being set.

So you can override the shipped syntax by having your script run early in 'runtimepath' (as you're already doing) and making sure that the b:current_syntax variable is set (to the name of the filetype used for syntax) at the end of the execution of your script.

See :help mysyntaxfile-replace:

If you don't like a distributed syntax file, or you have downloaded a new version, follow the same steps as for mysyntaxfile above. Just make sure that you write the syntax file in a directory that is early in 'runtimepath'. Vim will only load the first syntax file found, assuming that it sets b:current_syntax.

For the html.vim type, there's one extra caveat, that it also checks for a main_syntax variable, which is used when syntax/html.vim is being sourced as part of another filetype that embeds HTML (such as PHP!)

See this code snippet:

" quit when a syntax file was already loaded
if !exists("main_syntax")
  if exists("b:current_syntax")
  let main_syntax = 'html'

So, in order to override the bundled syntax/html.vim, you need to make sure that your script sets b:current_syntax (to "html"), and that it does not set (or unlets) the main_syntax variable, to ensure the bundled script will finish execution early.

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.