What is the recommended way to use Vim folding for Python code
Personally I can't convince myself to litter my code with the markers. I've become pretty used to (and efficient) at using indent-folding. Together with my mapping of space bar (see below) to open/close folds and the zR and zM commands, I'm right at home. Perfect for Python!
set foldmethod=indent
nnoremap <space> za
vnoremap <space> zf
I use this syntax file for Python. It sets the folding method to syntax and folds all classes and functions, but nothing else.
Yet another plugin for folding Python code. Rather simple, handling docstrings, and on the GitHub:
SimpylFold
Enjoy!
Python is well suited for folding on indent, bit for writing my own code I use markers as they can crunch a document down the way you want it and can serve as a kind of a table of contents. I have this in my vimrc to flip between the two when I'm viewing someone elses code.
#Toggle fold methods \fo
let g:FoldMethod = 0
map <leader>fo :call ToggleFold()<cr>
fun! ToggleFold()
if g:FoldMethod == 0
exe 'set foldmethod=indent'
let g:FoldMethod = 1
else
exe 'set foldmethod=marker'
let g:FoldMethod = 0
endif
endfun
#Add markers (trigger on class Foo line)
nnoremap ,f2 ^wywO#<c-r>0 {{{2<esc>
nnoremap ,f3 ^wywO#<c-r>0 {{{3<esc>
nnoremap ,f4 ^wywO#<c-r>0 {{{4<esc>
nnoremap ,f1 ^wywO#<c-r>0 {{{1<esc>