How do I stop vim reading in the default syntax?

Viewed 73

I have defined my own javascript syntax file, which is in ~/.vim/syntax/after/

The problem is that the default syntax file that exists in /usr/share/vim/vim82/syntax is loaded along side my custom syntax file (it seems to be loaded before my custom syntax).

The result is that it conflicts with my custom syntax.

How do I ONLY load my custom syntax file? Not the default one.

Of course I could just delete the default one (which works), but that seems like a not very robust fix to the problem considering it will just be put back there when vim is updated.

I have "syntax clear" at the top of my custom file but it doesn't help.

I also note that the default syntax file has the following at the top:

  if !exists("main_syntax")
    " quit when a syntax file was already loaded
    if exists("b:current_syntax")
      finish
    endif
    let main_syntax = 'javascript'
  elseif exists("b:current_syntax") && b:current_syntax == "javascript"
    finish
  endif

But that doesn't do anything either.

I have also tried putting the syntax folder in .vim/syntax, as suggested on the official vim page, but that doesn't seem to do anything at all (as in, it's not read at all).

Thankyou.

1 Answers

In principle, the order in which syntax scripts are sourced is the following:

~/.vim/syntax/foo.vim
$VIMRUNTIME/syntax/foo.vim
~/.vim/after/syntax/foo.vim

~/.vim/after/syntax/ comes last. It is not a good place if you are writing a complete replacement of a built-in syntax script because you have at least one syntax script sourced before yours. On the other hand, it is a good place if what you want to do is either add your own stuff or selectively override stuff that has been set earlier.

$VIMRUNTIME/syntax/ is not a good place either, because it is a systemwide location. Your customisation is supposed to happen in your $HOME, right?.

~/.vim/syntax/ comes first so it is the ideal place for custom syntax scripts, if you add the following line:

let b:current_syntax = "javascript"

This is because $VIMRUNTIME/syntax/javascript.vim begins with the following boilerplate:

if !exists("main_syntax")
  " quit when a syntax file was already loaded
  if exists("b:current_syntax")
    finish
  endif
  let main_syntax = 'javascript'
elseif exists("b:current_syntax") && b:current_syntax == "javascript"
  finish
endif

which, essentially, throws the towel if b:current_syntax is set, and ends with:

let b:current_syntax = "javascript"

to "mark its territory", if you will.

You don't have to check for anything because you come first, but you still have to tell other syntax scripts to mind their own business.

Note that bad mannered syntax scripts may not check for that buffer-local variable so it is entirely possible to still have conflicts even if you are doing the right thing. But that's the nature of Vim.

Related