Fold function in vim

VimFolding

Vim Problem Overview


Is there any way or tools to fold function in vim, like Visual Studio or Eclipse?

Vim Solutions


Solution 1 - Vim

    Vim folding commands
---------------------------------
zf#j creates a fold from the cursor down # lines.
zf/ string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
za toggle a fold at the cursor.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zc closes a fold under cursor. 
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.]z move to end of open fold.

Source: vim docs.

Solution 2 - Vim

Yes. VIM has exceptional folding capabilities. I don't like learning too many controls, I prefer automation, so here is what I personally use:

In my .vimrc:

set foldmethod=indent
set foldlevel=1
set foldclose=all

This automatically folds files that you open, based on indent, for everything indented for more than 1 level. The foldclose option makes the fold automatically re-close after I navigate out of the fold.

In-file Controls:

zo - opens folds
zc - closes fold
zm - increases auto fold depth
zr - reduces auto fold depth

And if you ever get annoyed with the folds, use

: set foldmethod=syntax

or press:

zR

to make them all go away.

Solution 3 - Vim

:set foldmethod=syntax

should fold all functions and other blocks automatically, if you have syntax file for your language.

Solution 4 - Vim

Vim has excellent folding support. There is good documentation in the vim help system. Just open vim and do

:help usr_28.txt 

After reading that you can also read

:help folding

for some more information.

Solution 5 - Vim

yes, it is bound to the 'z' key, e.g. zO opens all folds. see ":help fold" in vim for more information. You can do folding according to very simple rules, like indentation, or according to the code syntax.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYongwei XingView Question on Stackoverflow
Solution 1 - VimcodaddictView Answer on Stackoverflow
Solution 2 - VimbhekmanView Answer on Stackoverflow
Solution 3 - VimPaulView Answer on Stackoverflow
Solution 4 - VimNeg_EVView Answer on Stackoverflow
Solution 5 - VimBullet Tooth TonyView Answer on Stackoverflow