Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between `syntax on` and `syntax enable` in vimscript?

Tags:

vim

In my .vimrc file I use:

syntax on 

Today, I was perusing through some .vimrc files from other developers and i've notice a few using:

syntax enabled 

Is there a difference? Are these both accomplishing the same goal just using different locution?

like image 866
Brandon Crooks Avatar asked Oct 27 '15 23:10

Brandon Crooks


People also ask

How do I enable syntax in Vim?

To enable Syntax Highlighting feature in VI editor, open the file called /etc/profile. Add the alias function to VI by pointing to VIM in /etc/profile file. This file is used to set alias functions globally. If you would like to set user specific aliases and functions, then you need to open the file .

What is in Vim script?

Vim's scripting language, known as Vimscript, is a typical dynamic imperative language and offers most of the usual language features: variables, expressions, control structures, built-in functions, user-defined functions, first-class strings, high-level data structures (lists and dictionaries), terminal and file I/O, ...


1 Answers

What Vim Claims

For syntax on vs syntax enable, the help files claim:

The ":syntax enable" command will keep your current color settings.  This allows using ":highlight" commands to set your preferred colors before or after using this command.  If you want Vim to overrule your settings with the defaults, use: >     :syntax on 

I Can't Verify These Claims

The behavior I see in Vim does not appear to match the above help statement.

After testing locally with some empty .vimrcs and experimenting with on, enable, and placement of highlight commands, I can't figure out what Vim is actually doing (I tested with highlight ColorColumn guibg=#331111 and set colorcolumn=80). Highlighting is sometimes overwritten and sometimes not.

Only Let Vim Set Syntax Once

I no longer trust Vim, so I only let syntax get set once, ever. Here's what I have in my .vimrc:

if !exists("g:syntax_on")     syntax enable endif 

I use enable because of the above claim that it won't overwrite your settings, however it doesn't seem to make any difference when starting Vim.

More details

You can see that h g:syntax_on shows that on and enable source the same file:

Details: The ":syntax" commands are implemented by sourcing a file.  To see exactly how this works, look in the file:     command     file ~     :syntax enable  $VIMRUNTIME/syntax/syntax.vim     :syntax on      $VIMRUNTIME/syntax/syntax.vim 

If you're curious, g:syntax_on gets set in $VIMRUNTIME/syntax/synload.vim

Also running Vim with no plugins/settings vim -u NONE does NOT load any of the syntax files.

like image 168
Andy Ray Avatar answered Sep 20 '22 07:09

Andy Ray