Vim Set Tab to N Spaces
Want to convert the tab key to insert 2, 4 or 8 spaces instead of the tab character (\t
) in Vim? Let’s look at the settings we need to configure in order to achieve this:
:set expandtab
– Uses the appropriate number of spaces to insert instead of the tab character (\t
) when the tab key is hit. If it is disabled (:set noexpandtab
) then tab characters are used whenever you hit the tab key instead of spaces. Whether disabled the width of the tab (\t
) character is controlled by thetabstop
setting but when enabled, the width (number of spaces to insert instead of\t
) is controlled by thetabstop
andsofttabstop
setting (explained below).:set shiftwidth=2
– Theshiftwidth
setting controls the number of spaces that are inserted for indentation, i.e., you hit enter to go to the next line and expect that to be indented (one or more tabs inserted automatically). In this case the indentation will happen by2
spaces.:set tabstop=2
– Thetabstop
setting controls the width of the tab character (\t
) whenexpandtab
is disabled and the number of space characters to be inserted whenexpandtab
is enabled. In this case2
spaces will be inserted whenever you hit the tab key (ifexpandtab
is enabled).:set softtabstop=2
– Thesofttabstop
setting inserts the specified number of spaces when hitting the tab key. Kind of similar totabstop
but a couple of things to keep in mind:- Its value kind of overrides that of
tabstop
(for tab key hits). - It also removes the same number of spaces when you hit the backspace key. This is a must have!
- It will continue to insert spaces even if
set :noexpandtab
is set, which is supposed to insert tab (\t
) on hitting the tab key. - A value of
0
disables it and a negative value makes it fallback to the value ofshiftwidth
.
- Its value kind of overrides that of
This is what the docs says about each setting:
expandtab
– In Insert mode: Use the appropriate number of spaces to insert a<Tab>
.shiftwidth
– Number of spaces to use for each step of (auto)indent. Used for'cindent'
,>>
,<<
, etc.tabstop
– Number of spaces that a<Tab>
in the file counts for.softtabstop
– Number of spaces that a<Tab>
counts for while performing editing operations, like inserting a<Tab>
or using<BS>
.
So to sum it up, either execute the following set
commands when the editor is open:
:set expandtab shiftwidth=2 tabstop=2 softtabstop=2
" Or the short form
:set et sw=2 ts=2 sts=2
Or you can add the following in your ~/.vimrc
which is automatically sourced during Vim’s loading sequence:
set expandtab
set shiftwidth=2
set tabstop=2
set softtabstop=2
Note: In the examples above, I’ve always used 2
as the number of spaces to be inserted when the tab key is hit. You can change that to 4
or 8
or whatever you want.
Bonus: All the settings above will affect the tabs you insert after they’ve been set. What about the pre-existing tab indentations? You’d want to replace them with spaces as well right? To do this, just hit the :retab
command which will replace all tabs with tabstop
number of spaces.
" If tabstop=4 then replace all tabs with 4 spaces
:retab
" Set tabstop=2 and then replace all tabs with 2 spaces
:retab 2