Skip to main content
added 77 characters in body
Source Link
romainl
  • 42.3k
  • 5
  • 87
  • 118
  • If you want to enable syntax highlighting, put syntax on in your vimrc, where it belongs.
  • If you want to assign a specific syntax different from the one that is automatically inferred from the filetype to the current buffer, put set syntax=<whatever> in your ftplugin. Since you didn't hint otherwise, you probably don't need this.

This one:

set syntax=test

looks much better.

  • If you want to enable syntax highlighting, put syntax on in your vimrc, where it belongs.
  • If you want to assign a specific syntax to the current buffer, put set syntax=<whatever> in your ftplugin.

This one:

set syntax=test

looks much better.

  • If you want to enable syntax highlighting, put syntax on in your vimrc, where it belongs.
  • If you want to assign a specific syntax different from the one that is automatically inferred from the filetype to the current buffer, put set syntax=<whatever> in your ftplugin. Since you didn't hint otherwise, you probably don't need this.
added 533 characters in body
Source Link
romainl
  • 42.3k
  • 5
  • 87
  • 118
  1. The FileType event is triggered with value test.

    The FileType event is triggered with value test.

  2. ftplugin/test.vim is sourced.

    ftplugin/test.vim is sourced.

  3. A number of autocommands are added for the FileType event with value test.

    A number of autocommands are added for the FileType event with value test.

  4. None of the desired options are set for the current buffer.

    None of the desired options are set for the current buffer and the number of test autocommands augments as you edit more .test files:

    :e 1.test
      - no options
      - 4 "test" autocommands
    :e 2.test
      - options are set because of the autocommands
      - 8 "test" autocommands
    :e 3.test
      - options are set 2 times because of the autocommands
      - 12 "test" autocommands
    :e 4.test
      - options are set 3 times because of the autocommands
      - 16 "test" autocommands
    etc.
    

That is not good.

The fix is thus to usewrite your ftplugin properly:

  1. The FileType event is triggered with value test.
  2. ftplugin/test.vim is sourced.
  3. A number of autocommands are added for the FileType event with value test.
  4. None of the desired options are set for the current buffer.

The fix is thus to use your ftplugin properly:

  1. The FileType event is triggered with value test.

  2. ftplugin/test.vim is sourced.

  3. A number of autocommands are added for the FileType event with value test.

  4. None of the desired options are set for the current buffer and the number of test autocommands augments as you edit more .test files:

    :e 1.test
      - no options
      - 4 "test" autocommands
    :e 2.test
      - options are set because of the autocommands
      - 8 "test" autocommands
    :e 3.test
      - options are set 2 times because of the autocommands
      - 12 "test" autocommands
    :e 4.test
      - options are set 3 times because of the autocommands
      - 16 "test" autocommands
    etc.
    

That is not good.

The fix is thus to write your ftplugin properly:

Source Link
romainl
  • 42.3k
  • 5
  • 87
  • 118

Everything is "working as advertised".

To expand on @Matt's answer…

There are two ways to execute commands, set options, etc. for a given filetype.

  • One is to use autocommands on the FileType event:

    autocommand FileType test setlocal expandtab
    

    where the command :setlocal expandtab is executed when the FileType event is triggered with value test.

    This is generally what people use in their vimrc when they want to keep all their stuff together in a single file.

  • The other is to simply put the desired commands in a dedicated "ftplugin":

    " in ftplugin/test.vim
    setlocal expandtab
    

    where ftplugin/test.vim is sourced automatically by Vim when the FileType event is triggered with value test.

    This is rather obviously less verbose than the first method, but it also has other advantages:

    • it uses Vim's built-in mechanism in the most canonical way possible
    • it doesn't add n more autocommands to an already towering pile of autocommands, which can hurt performance if it is not done correctly.

Basically, what you tried doesn't work because it is a mish-mash of the two methods above, where you add FileType autocommands where they don't make sense.

Here is what should happen:

  1. The FileType event is triggered with value test.
  2. ftplugin/test.vim is sourced.
  3. A bunch of options are set for the current buffer.

Here is what actually happens:

  1. The FileType event is triggered with value test.
  2. ftplugin/test.vim is sourced.
  3. A number of autocommands are added for the FileType event with value test.
  4. None of the desired options are set for the current buffer.

The fix is thus to use your ftplugin properly:

setlocal expandtab
setlocal shiftwidth=2
setlocal softtabstop=2
setlocal cinoptions=(0

Note that the following command is incorrect for a few reasons:

setlocal syntax=on
  • You are confusing the :help :syntax command and the :help 'syntax' option.
  • Syntax highlighting is a global feature, so there is no point in trying to make it local.
  • The syntax option is local anyway so setlocal is not necessary.
  • You are assigning the on syntax to your test buffer, which is certainly not the desired outcome.

So…

  • If you want to enable syntax highlighting, put syntax on in your vimrc, where it belongs.
  • If you want to assign a specific syntax to the current buffer, put set syntax=<whatever> in your ftplugin.

This one:

set syntax=test

looks much better.