Although loads of editors have the ability, I’ve never used code folding before. I’ve seen the point, but clicking little arrows or plus signs with the mouse always seemed just a little too annoying. Like most things in Vim, code folding is very customisable, which allowed me to get it just the way I want it. The default behaviour folds code too much (at least when coding Ruby) to be useful for me, so I had to chuck a couple of lines into my .vimrc file to sort that out and tell Vim to automatically define folds by syntax:

set foldmethod=syntax set foldnestmax=5

Presumably, you need some Ruby syntax highlighting installed for that to work. I compiled in Ruby support when I built Vim.

This is all tickety-boo except that all the folds default to closed when you open a file. To make sure all folds are open when we open a new buffer, I added this auto-command:

autocmd BufEnter * exe "normal zR"

Sorted. The core folding commands for me are:

zo
Open the fold on the same line as the cursor
zc
Close the fold that the cursor is inside
zR
Open all folds
zM
Close all folds

As usual, the docs have way more.

Leave a Reply