What is the recommended way to use Vim folding for Python code

后端 未结 11 2090
自闭症患者
自闭症患者 2020-12-22 16:33

I am interested in enabling code folding in Vim for Python code. I have noticed multiple ways to do so.

Does anyone have a preferred way to do Python code folding in

11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-22 17:07

    I really like this little vim script i wrote for .vimrc. It maps alt+1 to fold the first python indent level (class definitions and functions), alt+2 to fold the second level (class methods), and alt+0 to unfold everything. It makes sure it only folds one level and doesn't fold any of the nested sub levels. You can still use za to toggle folding for the current block. Note that in ^[0, the ^[ is alt for my terminal. Don't have a lot of experience in vim script so feel free to make suggestions on the function :)

    " Python folding
    nnoremap ^[0 zR
    nnoremap ^[1 :call Fold(0)
    nnoremap ^[2 :call Fold(1)
    function Fold(level)
        :let b:max = a:level + 1
        :set foldmethod=indent
        :execute 'set foldnestmax='.b:max
        :execute 'set foldlevel='.a:level
    endfunction
    

提交回复
热议问题