Search code examples
vimcolor-scheme

What is the reason for the `if exists("syntax_on")` guard in VIM colorschemes?


Just about every VIM colorscheme has this snippet in it:

if exists("syntax_on")
  syntax reset
endif

This is also listed as a "recommended" thing to put in colorschemes in all sorts of tutorials, etc.

However, I've experimented quite a bit with the difference between that, and just a plain:

syntax reset

and I can't find any difference. If syntax is enabled, this command correctly does the usual syntax reset magic, but if syntax is off, it doesn't appear to have any effect, or give any error. What's more, reading the whole section of the VIM manual about syntax highlighting and colorschemes, I can't see any reason why a plain, unguarded syntax reset would be harmful if "syntax_on" isn't set.

If that's truly the case, what is the purpose of the if exists("syntax_on") guard? Is it because this used to be required in a previous version of VIM, but is no longer necessary in at least 7.3? Or is there some subtle corner case I'm missing?


Solution

  • When syntax_on is not defined, we don't need to run syntax reset which is the same as:

    g:syntax_cmd = 'reset'
    runtime! syntax/syncolor.vim
    

    It makes vim faster.