How can I automatically fold all functions in a file with vim?

Yongwei Xing picture Yongwei Xing · Dec 30, 2010 · Viewed 35.8k times · Source

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.

Answer

R. Martinho Fernandes picture R. Martinho Fernandes · Dec 30, 2010

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