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

前端 未结 1 353
灰色年华
灰色年华 2020-12-24 11:16

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:

相关标签:
1条回答
  • 2020-12-24 12:04

    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.

    0 讨论(0)
提交回复
热议问题