31

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?

3 Answers 3

23

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.

3
  • This is what I was looking for. Currently debating between setting ts as 2, 4, or 8
    – user341814
    Jul 9, 2014 at 3:19
  • I use 8 on non-code files, and either 2 or 4 depending on the language and how much nesting tends to occur.
    – Heptite
    Jul 9, 2014 at 3:28
  • I only do C at the moment.
    – user341814
    Jul 9, 2014 at 5:17
48

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 by tabstop. 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 :

  1. Inserting a tabulation in the file would produce an indenting 4 spaces wide.
  2. As your tabstop is set to 2, this would actually be the equivalent of 2 tabulations. This is fairly easy to check, just use the list and listchars options to display tabulations.
  3. If you indent a line using >>, the indentation would be 8 spaces wide (hence, the equivalent of 4 tabulations, based on tabstop value, same as above).

enter image description here

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
-1

According to http://vim.wikia.com/wiki/Indenting_source_code, the 'filetype plugin indent on' command will cause the program to use filetype-specific indentation scripts located in the indent subdirectory of the Vim installation. That page also states that 'cindent' is used automatically in C and C++ files, and that you should not be required to use that command manually.

I am not very familiar with Vim, as I only use it for some occasional basic text-editing, but I would try manually issuing the 'cindent' command to use Vim's default autoindent size for that code. If that doesn't work, you could try 'filetype plugin indent on' and edit the scripts yourself to get the desired indents.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .