0

I've always used vim writing perl and I'm starting to transition to python but I see people saying that hitting space 4 times is unnecessary if you choose the right editor, where tabs are converted to spaces.

I love vim and would prefer not to have to move. I have seen this SO thread from 5 years ago and want to know if this is still the best way to achieve it or is there a simpler/newer way?

Tab key == 4 spaces and auto-indent after curly braces in Vim

Community
  • 1
  • 1
Daniel
  • 289
  • 2
  • 14
  • What's wrong with that answer? It's just 4 lines, and you don't even need to type them in everytime if you put them in the .vimrc file. – JJJ Nov 07 '13 at 12:28
  • I was mainly wondering if it's still relevant or needed after 5 years. Do newer versions of vim have built in functions which would make this redundant or not work. – Daniel Nov 07 '13 at 18:16
  • That IS the built-in function of Vim. You're just setting options to tell Vim which built-in behavior to use. Not everybody wants 4 spaces when they hit tab. It's just like hitting a checkbox in the settings dialog of a point-and-click editor. – Ben Nov 07 '13 at 21:17

2 Answers2

4

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
glts
  • 21,808
  • 12
  • 73
  • 94
  • 2
    Instead of filling the `.vimrc`, it is better to use the filetype configuration file, like `~/.vim/ftplugin/py.vim`, as [explained in Vim Faq.](http://vimhelp.appspot.com/vim_faq.txt.html#faq-26.8) – mMontu Nov 07 '13 at 14:55
  • @mMontu It's a matter of personal preference, isn't it? Or are there objective reasons that user filetype plugins are better? (By the way, I think you wanted to link section 26.1.) – glts Nov 07 '13 at 15:48
  • Section 26.1 also has relevant information, but I linked to 26.8 because I thought the answer was overriding the default filetype plugin (in `$VIMRUNTIME/ftplugin/python.vim`) - I forgot to add the `../after/..` directory in the example of the previous comment. On that section Vim FAQ states that the filetype file is a better alternative, what I agree as it allow better organization, but it probably boils down to a matter of taste, as you said. – mMontu Nov 07 '13 at 16:25
  • +1 @mMontu, i prefer breaking out the customization into the provided vim directory structure rather than cluttering the vimrc. it's also a good excercise for understanding vim a little better. – randomfigure Nov 08 '13 at 01:53
0

I just use:

set sw=4 ts=4 expandtab

You can make it work for Python only:

au FileType python setlocal tabstop=4 shiftwidth=4 expandtab
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107