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:
- The
FileType
event is triggered with value test
.
ftplugin/test.vim
is sourced.
- A bunch of options are set for the current buffer.
Here is what actually happens:
- The
FileType
event is triggered with value test
.
ftplugin/test.vim
is sourced.
- A number of autocommands are added for the
FileType
event with value test
.
- 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.