Sunday, March 6, 2011

What is the recommended way to use vim folding for python coding

I am interested in enabling code folding in vim for python coding.

I have noticed multiple ways to do so.

Does anyone have a preferred way to do python code folding in vim?
I.e,

  • do you have a particular vim plugin that you use and like?
  • do you use manual folding or do you place markers in comments?
  • any other recommended ways to do code folding for python in vim?
From stackoverflow
  • To be honest folding is one of the vim features I haven't quite used. I tend to use split panes alot more, you can access them with

    :split
    

    and

    :vsplit
    

    And move around them with

    ctrl+w
    

    By default the split pane contains the same buffer that is currently in focus, but you can also open another buffer in it:

    :split otherbuffer
    

    I usually open two panes for the same file when I'm working on different sections of it, so I effectively use that in lieu of folding.

    I realize this answer is slightly offtopic, but I thought you might find the tip on split panes useful.

  • 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>
    
  • I really like this this plugin.

    Paul D. Eden : Unfortunately, this plugin uses more processor time than I would prefer (even with the a version) as I like to keep my vim speedy. Any other suggestions?
  • Try this plugin:

    http://vim.sourceforge.net/scripts/script.php?script_id=515

  • 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!

    nnoremap <space> za

    vnoremap <space> zf

    tmadsen : :set foldmethod=indent first. I was googling for just this and stumbled upon your answer, very nice! :)
  • I use this syntax file for Python. It sets the folding method to syntax and folds all classes and functions, but nothing else.

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

  • I wrote my own python ftplugin -- habamax.ru/blog/2009/05/python-folding-in-vim/

    It could be slow as it uses 'expr' foldmethod which is slow by default.

  • 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 comments:

Post a Comment