For me the ideal folding is to fold just the class
and def
blocks, indent folding is too much for my taste. I think one elegant solution is to use the syntax system like this one mentioned by Tomas. However, this one is meant to replace the original syntax file and it may end being older than the original (i.e. that script doesn't mention Python 3 syntax).
My solution is to place in the ~/.vim/syntax
folder a file named python.vim
with just the important lines (taken from the above script):
syn match pythonDefStatement /^\s*\%(def\|class\)/
\ nextgroup=pythonFunction skipwhite
syn region pythonFunctionFold start="^\z(\s*\)\%(def\|class\)\>"
\ end="\ze\%(\s*\n\)\+\%(\z1\s\)\@!." fold transparent
hi link pythonDefStatement Statement
Then simply activate the folding with :set foldmethod=syntax
.