78

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

Yongwei Xing
  • 12,983
  • 24
  • 70
  • 90

5 Answers5

145
    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.

shmup
  • 884
  • 9
  • 19
codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 3
    zc closes fold under cursor. (warning: figured it by experimenting) – Srikanth Dec 19 '10 at 18:50
  • 4
    za will toggle a fold (open if currently closed, and vice versa). I find that pretty handy; almost never use zc or zo. http://vim.wikia.com/wiki/Folding#Opening_and_closing_folds – Aman Apr 09 '14 at 19:36
  • 2
    `zfgg` folds all lines from top to cursor. `zfG` folds all lines from cursor to bottom. – Christopher Markieta Oct 20 '15 at 20:44
  • 4
    You can also select multiple lines in Visual Line Mode (`Shift+v`) and fold them with `zf`. – Christopher Markieta Oct 20 '15 at 21:02
  • 1
    I used zf% to fold a C function effectively (I don't have a syntax plugin that knows how to do it automatically) – Paul Stelian Aug 28 '19 at 07:26
  • Actually, as with so many other commands, "zf" ends with a "cursor movement". Example: zfH folds from the cursor line to the top of the screen. Thus, parking the cursor in an opening "{" and typing "zf%" folds through the end of the block ("%" in an opening cracket or tab jumps to the closing one). – Juan Lanus Apr 20 '20 at 22:56
81

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.

bhekman
  • 3,227
  • 21
  • 24
  • 4
    I find useful to have: `set nofoldenable` `set foldlevel=99` too. It disables auto folding on opening file and folds only outer functions (not stepping in and folding "fors" and "ifs" and what not) Made my config based on your answer though, thanks. – merinoff Jul 06 '17 at 16:43
  • 2
    @merinoff Although it will likely never matter, a setting of `foldlevel` greater than 20 has no effect due to that being the internal cap, according to the documentation (for version 8.1). – Jamie S Oct 16 '18 at 21:55
  • 1
    After trying out a lot of plugins for a lot of file types, this is the best I found. Clean, easy, no plugins. Thank a lot. – Phúc Lê Sep 03 '19 at 14:31
  • You do not know the level of excitement I felt when I saw this solution. – Dogunbound hounds Nov 28 '21 at 23:59
25
:set foldmethod=syntax

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

Paul
  • 13,042
  • 3
  • 41
  • 59
5

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.

Vini.g.fer
  • 11,639
  • 16
  • 61
  • 90
Neg_EV
  • 1,930
  • 1
  • 15
  • 23
2

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.