At moment, mostly for random reasons, I have
$ cat ~/.vim/ftplugin/c.vim
set tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab smartindent autoindent cinoptions=g2
and this
$ cat ~/.vim/ftplugin/cpp.vim
let c_no_curly_error = 1
set tabstop=8 softtabstop=8 expandtab shiftwidth=8 smarttab smartindent autoindent cinoptions=g2
set mps+=<:>
where I've put 2
s and 8
s in the two files for debugging purpose.
Since :echo &tabstop
gives 2
when I open a *.cpp
file (and also when I open a *.c
file, which is expected), I'm pretty sure I've put those files in the wrong place or, at least, some lines of those files in the wrong file and wrong place.
In Chapter 42 from Learn Vimscript the Hard Way I read the following
~/.vim/indent/
Files in
~/.vim/indent/
are a lot likeftplugin
files. They get loaded based on their names.
indent
files should set options related to indentation for their filetypes, and those options should be buffer-local.Yes, you could simply put this code in the
ftplugin
files, but it's better to separate it out so other Vim users will understand what you're doing. It's just a convention, but please be a considerate plugin author and follow it.
which tells me that, on the one hand, ~/.vim/indent/
would be a better place for filetype-specific Vim files containing set tabstop=... ...
, and on the other hand tells me that ~/.vim/ftplugin/
isn't that bad.
Nonetheless, If I move those two files from ~/.vim/ftplugin/
to ~/.vim/indent/
, then :echo &tabstop
gives the correct output of 8
for *.cpp
files and 2
for *.c
files.
This troubleshooting tells me that probably ~/.vim/ftplugin/
is a wrong place to put that setting, and ~/.vim/indent
is maybe the right place.
- Is this the case?
If the answer is affirmative, then I know where I have to move c.vim
into, and that I have to create a cpp.vim
file beside that containing only the second line from the cpp.vim
which is above.
- As a side question, where should I put the other two options, which are not indentation specific? What is their most appropriate location?
setlocal
instead ofset
?setlocal
(in both thec.vim
andcpp.vim
files). On the other hand many files in/usr/share/vim/vim82/ftplugin/
and usesetlocal
, so doing this change is probably good for some other good reason.setlocal
keeps the change local to that buffer (relevant if you have many buffers with different filetypes!). For personal stuff,after
is also a good way to be sure your settings take effect no matter what