Quickly Folding Functions In Vim

Vim IconThe is really a Vim tip on how to fold any code block including functions. It’s really helpful if you want to collapse a function or any other type of code block. Best of all, it is quick and easy to do.

Navigate your cursor somewhere inside of the code block you want to fold. You do not have to have your cursor on the opening brace, anywhere in the block is fine. Make sure you are in command/normal mode and type:

zfa}

To save your folds between vim sessions you need to issue the command :mkview otherwise when you close vim your folds will be lost (your folds, not your code). To make life easier on you, you can have your folds automatically saved for you by adding this to your .vimrc file

au BufWinLeave * mkview
au BufWinEnter * silent loadview

Leave a Reply

Your email address will not be published. Required fields are marked *