How can I automatically fold a long C code in Vim?

Léo Léopold Hertz 준영 picture Léo Léopold Hertz 준영 · Apr 17, 2009 · Viewed 9.9k times · Source

I regularly run into C-codes without folding. It is irritating to read them if there is no folding, particularly with long files. How can I fold them?

Answer

Andy picture Andy · Apr 17, 2009

To fold according to syntax

:set foldmethod=syntax

If you want to do it manually on the bits you want to fold away

:set foldmethod=manual

then create new folds by selecting / moving and pressing zf e.g.

shift-v j j zf

(ignoring the spaces)

Edit: Also see the comments of this answer for indent and marker foldmethods.