At first, I use the set foldmethod=marker, and move the cursor to the { of one function, use the zf% to fold current function. But there are a lot of functions in this file. How can I fold all functions in this file? And I don't want to fold {} in the functions.
-
4FYI vim distributions usually come with embedded help. If you type `:help fold` you can see all the fold related stuff. – R. Martinho Fernandes Dec 30 '10 at 02:45
-
2You'll have to add what language you're interested in (not all languages have {} for a start!); folding in Vim is a topic of its own. – Rook Feb 05 '12 at 01:19
6 Answers
If you :set foldmethod=syntax
the folds will be specified from the syntax definitions. If you prefer you can :set foldmethod=indent
to have the indentation define the folds.
You can close all folds with zM
. If you have nested folds and you want to fold level by level, use zm
. To open folds use zR
(all) and zr
(level by level).

- 228,013
- 71
- 433
- 510
-
1I think you misunderstand my meaning. At first, I need set up fold for each function. I just want to know how to set up fold for all functions in the first one time. – Yongwei Xing Dec 30 '10 at 03:00
-
@Yongwei: ok, I think I get it now. Is this what you wanted? – R. Martinho Fernandes Dec 30 '10 at 03:08
-
5
-
8For those who wonder how to remember `zm` and `zr`, the [official documentation](http://vimdoc.sourceforge.net/htmldoc/fold.html) says it is "More" and "Reduce". (Still not quite intuitive, but as most things in vim world, it is what it is.) – RayLuo Feb 07 '17 at 21:49
-
4Just for info, in their documentation, they have mentioned that all the folding commands starts with 'z' . It looks like folded paper if looked from sides. – hunch Jun 16 '17 at 16:25
-
When I try `zR` I get put in replace mode, and `zM` says no fold found (even though I can fold individually with just `z`). The only line in my rc modifying z is `nnoremap z za` Any clues? – donkey Jun 21 '19 at 14:10
-
As a mnemonic, I actually prefer referring to `M` as `Mask` and `R` as `Reveal` as I find it more intuitive than more/reduce. – Omar Jun 29 '20 at 16:32
-
How to make it working for all files? I added `set foldmethod=indent` into `.vimrc` but it works in files where I manually set command `:set foldmethod=indent` – bora89 Mar 23 '23 at 07:59
If each function has its opening brace on the first column you could do:
:%g/^{/normal! zf%
Maybe it is more clear this way:
:%g /^{/ normal! zf%
the g
command selects lines according to the following pattern, and executes an ex command (here normal!
to play normal mode keystrokes).
See :help :g
and :help :normal

- 76,634
- 23
- 210
- 236
-
I use this - thanks however how can you avoid it from folding across functions? it seems it is not only functions – serup Sep 16 '16 at 16:36
I was trying to do the same and ended up simply doing:
setlocal foldmethod=marker
setlocal foldmarker={,}
It uses the marker fold method and changes the marker to a single curly brace (by default the marker is {{{
).

- 1,402
- 14
- 26
-
I loved that, quite handy! But I should split lines which closes one brace and opens another (like "} else {") – Serhat Cevikel Jun 02 '17 at 01:03
I came across this when I was searching for a similar thing. You would have obviously figured this out by now, but for the benefit of other people i'll answer it anyway.
You need to put in the following lines in your .vimrc:
set foldmethod=syntax
set foldnestmax=1

- 91
- 1
- 1
set foldlevel=0
will fold everything from the start, what is ment to be folded. Depending on the language, and your fold function, the contents of fold will vary.

- 60,248
- 49
- 165
- 242
Try :%g/\(function\_.\{-}\)\@<={/ normal! f{zf%
Explain bit by bit:
:%g
- search globaly in a whole file
/\(function\_.\{-}\)\@<={/
- pattern to find first '{' after any 'function' and put cursor on begin of string with that '{'
normal! f{zf%
- execute forward to '{' f{
and make fold with move '%' zf%
on that string

- 41
- 2