Vim tab size when hitting <Tab> and when autoindent applies
I decided to change my tab size from 4 to 2, because why not? Anyone else who wants to look at the code can surely use thier preferences.
However, a question.
If I hit the tab key, it inserts 2, but Vim's autoindent is still putting 4. What do I change to make that 2?
Another unrelated question: Which indent style is best for C and similar languages? I have been using 1TBS, but there are so many to choose from. Are any of them more professional or preferred?
Try setting 'shiftwidth
' to the same value as 'tabstop
'. Better yet, if you're using a recent enough version of Vim, set 'shiftwidth
' to 0 and it will default to whatever 'tabstop
' is set to.
Vim indentation options
Vim mainly uses 3 settings as for the indenting size :
-
tabstop
,ts
: when Vim encounters a tabulation in a file you're opening, it displays the tab as {ts} spaces (see tabstop help, or type:help tabstop
in Vim). -
softtabstop
,sts
: when you're editing a file and press the tab key, Vim uses this setting to define the width of the inserted tabulation (see softtabstop help, or type:help softtabstop
in Vim). -
shiftwidth
,sw
: the number of spaces Vim uses when indenting, either using autoindent stuff or the usual>>
,<<
commands. As Heptite noticed, this is what you were looking for in this particular case. And recent versions of Vim indeed allow you not to define this option,shiftwidth
would then take the value defined bytabstop
. Quite handy (see shiftwidth help).
Example
So for instance, if you use the following settings :
set sts=4
set ts=2
set sw=8
These would produce the following behaviour :
- Inserting a tabulation in the file would produce an indenting 4 spaces wide.
- As your
tabstop
is set to 2, this would actually be the equivalent of 2 tabulations. This is fairly easy to check, just use thelist
andlistchars
options to display tabulations. - If you indent a line using
>>
, the indentation would be 8 spaces wide (hence, the equivalent of 4 tabulations, based ontabstop
value, same as above).
Vim indentation recommendations (from Vim documentation)
From tabstop
help (:help tabstop
in Vim) :
There are four main ways to use tabs in Vim:
1. Always keep 'tabstop' at 8, set 'softtabstop' and 'shiftwidth' to 4
(or 3 or whatever you prefer) and use 'noexpandtab'. Then Vim
will use a mix of tabs and spaces, but typing <Tab> and <BS> will
behave like a tab appears every 4 (or 3) characters.
2. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use
'expandtab'. This way you will always insert spaces. The
formatting will never be messed up when 'tabstop' is changed.
3. Set 'tabstop' and 'shiftwidth' to whatever you prefer and use a
|modeline| to set these values when editing the file again. Only
works when using Vim to edit the file.
4. Always set 'tabstop' and 'shiftwidth' to the same value, and
'noexpandtab'. This should then work (for initial indents only)
for any tabstop setting that people use. It might be nice to have
tabs after the first non-blank inserted as spaces if you do this
though. Otherwise aligned comments will be wrong when 'tabstop' is
changed.
I personally use mainly the second solution, with 2 spaces wide tabulations.
set ts=2
set sts=2
set et "expand tabs to spaces