Folding in Vim

Folding in Vim is really powerful. If you want to dig into the dark side of Vim, then folding is an useful feature to check out. Here is how I use this feature.

Python code folding

One can use python_fold script to fold Python code.

Python code will look something like this:

Python fold

JavaScript fold

Add this to your vimrc to enable folding of JavaScript code:

function! JavaScriptFold() 
    setl foldmethod=syntax
    setl foldlevelstart=1
    syn region foldBraces start=/{/ end=/}/ transparent fold keepend extend

    function! FoldText()
        return substitute(getline(v:foldstart), '{.*', '{...}', '')
    setl foldtext=FoldText()
au FileType javascript call JavaScriptFold()
au FileType javascript setl fen

JavaScript code will look something like this:

JavaScript fold

13. Apr 2007 Code · Tips · VIM Editor
© Amir Salihefendic