" TL;DRtabstop " length of tabs
shiftwidth " length of indents in spaces
" when there are more spaces than the length of a tab
" you will get a tab
expandtab " insert a tab and get spaces
softtabstop " length of tabs when performing tab-related operations
" such as hitting the tab key or the delete
" when there are more spaces than the length of a tab
" you will get a tab
If you want to change the length of a tab in, say, Word, you change its tabstop value, but since programs all have their own tabstop value, your file may look different elsewhere, and if that’s fine for you, you should definitely use this since this is the easiest one and all it takes is just set tabstop=n
and using tabs going forward. This way the file will always be displayed just the way people like it (if they have consciously setup their tabstop value too, of course).
One thing to be noted here is that Vim allows you to have different values for tabs (tabstop) and indents (shiftwidth), that is, you can setup Vim in such a way that if you hit the tab key, there is a 4 characters long tab but if you hit the enter key, there will be 8 spaces (by default) to indent the new line. So if you change the value of tabstop, you may also want to set shiftwidth=n
But since you don’t always find a way to change tabstop and you don’t want your file to look like a mess elsewhere, you may want to use spaces instead. However, typing spaces one by one can be laborious, if not inaccurate. That’s where you will need Vim to expandtab for you. With set expandtab
whenever you hit tab, n
spaces instead of a real tab (\t) will be inserted.
In addition to using all tabs or all spaces, there is a hybrid solution, softtabstop, which allows you to change the behaviour of your tab key, that is, you can keep tabstop at 8 so Vim will display tabs as 8 characters long but if you hit the tab key, you can insert 4 spaces instead a tab and make your code more consistent with the rest of the Unix world and if you hit the tab key a couple times so that there are enough spaces to make up a tab, you will eventually get a tab instead.
However, there are more and more editors out there that do not use 8 as their default tabstop value and there are more and more different coding styles. Use an 8-space tabstop
and a 4-space softtabstop
and your code may eventually looks like
:set tabstop=8 softtabstop=4
Therefore, my suggestion is that you should avoid using softtabstop
if you can and use either
" simply change tabstop and use tabs consistently
set tabstop=4 " use the one you like
set shiftwidth=4 " 8 by default
set noexpandtab
or
" change tabstop and expand all tabs into actual spaces
" your code will look exactly the same everywhere
" you may have to deal with conflicts
" if you open a file full of tabs
" but maybe you can try :retab then?
set tabstop=4
set shiftwidth=4 " 8 by default
set expandtab
P.S. I just missed one important use of softtabstop
. By setting it to the same value as tabstop
, you can easily delete spaces as if they were tabs.