In my experience, the best settings for tabs and spaces in Python are these:
:set tabstop=8 shiftwidth=4 softtabstop=4 expandtab shiftround
Explanation:
Since you won't be using real tab characters at all, I find it's good to be explicit and leave them at their traditional width of 8 columns, tabstop=8
. You can leave this setting away, since it's the default anyway.
shiftwidth=4
and softtabstop=4
together with expandtab
make for a consistent tabbing experience both in insert mode as well as with indenting commands like >
and <
. They ensure that you can use the tab key to indent but Vim will always use 4 spaces to implement the indent.
Finally, shiftround
is optional: whenever you land on some odd column, say column 11, any indentation operation rounds to the next "tabstop". Indenting more will go to column 13 (past column 12: the third "tabstop"), and outdenting will go to column 9 (past column 8: the second "tabstop")
Like @elias said, if you want to make these settings permanent set up an :autocmd
in your vimrc:
autocmd FileType python setlocal ts=8 sw=4 sts=4 et sr