Way Better Python Indentation In Vim

Just wanted to share this tip I just stumbled across:

If you code in Python, and in your ~/.vimrc file you have ‘set smartindent’, change that to ‘set nosmartindent’ and add the following two lines (I’m told it can be combined into a one-liner, but I use two lines, so…)

filetype plugin on
filetype indent on

The indentation support for Python is much better. What I need to do now is write a little vim-fu that will tell vim to decrease the line indent if I enter two newlines or something, since there are no block delimeters (like braces or brackets) to tell vim about the block level.

  • http://moreati.org.uk/blog moreati

    I’ve was hoping this would make vim work the way I expect it to, but either I’ve done it wrong or my expectation is off. With this tip (or with smartindent), if you define a function and press return does vim indent the new line for you? I.e.

    def foo():
    ____<- Cursor appears here


  • m0j0

    When I do that, with the settings I’ve posted, the right thing happens. Indentation level is increased appropriately. Make sure you explicitly say ‘filetype indent on’, and make sure you also explicitly do ‘set nosmartindent’. And make sure you don’t have smartindent lurking in some other line in your ~/.vimrc like in an ‘autocmd’ line or something. To be safe, maybe just run “%s/\bsmartindent\b/\bnosmartindent\b/g’ or something like that (I didn’t test that, but if you don’t bound the term you’ll change “nosmartindent” to “nonosmartindent” so be careful).

  • http://moreati.org.uk/blog Alex Willmer

    Thank you, it was indeed a lingering .vimrc line.