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

后端 未结 11 1992
自闭症患者
自闭症患者 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 16:51

    I think that indent folding is fine for python. I'm making a multi-branched git repo for vim-config python/django IDE ideas. Fork away!

    http://github.com/skyl/vim-config-python-ide

    0 讨论(0)
  • 2020-12-22 16:52

    The Python source comes with a vim syntax plugin along with a custom vimrc file. Check the python FAQ on vim

    0 讨论(0)
  • 2020-12-22 16:59

    I use this syntax file for Python. It sets the folding method to syntax and folds all classes and functions, but nothing else.

    0 讨论(0)
  • 2020-12-22 17:03

    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>
    
    0 讨论(0)
  • 2020-12-22 17:06

    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.

    0 讨论(0)
  • 2020-12-22 17:06

    In your .vimrc:

    set foldmethod=indent
    set shiftwidth=4
    

    Then zM to mask all zR to expand all. I also added:

    nnoremap <space> za
    vnoremap <space> zf
    map z1  :set foldlevel=0<CR><Esc>
    map z2  :set foldlevel=1<CR><Esc>
    map z3  :set foldlevel=2<CR><Esc>
    map z4  :set foldlevel=3<CR><Esc>
    map z5  :set foldlevel=4<CR><Esc>
    map z6  :set foldlevel=5<CR><Esc>
    map z7  :set foldlevel=6<CR><Esc>
    map z8  :set foldlevel=7<CR><Esc>
    map z9  :set foldlevel=8<CR><Esc>
    

    So you can z1 and z2 to unindent little by little.

    0 讨论(0)
提交回复
热议问题