Question

I often come across the situation where I have lots of lines folded and I am writing, say a new block of code, above these folds. As soon as I type a '{', all the folds below open up. Even though it is legitimate that vim does this, it is irritating to close all the folds again. Is there a way around this situation?

Was it helpful?

Solution

I had the same problem and could solve it using this vimtip.

Little excerpt of the tip description:

If you are using any sort of automatic folding method, be it marker, syntax, or expression folding, inserting text that starts a fold will automatically open all folds beneath the insertion point. This can be very annoying. To get around this, you can temporarily switch to a manual fold method when entering insert mode, and switch back when leaving it.

The trick is to set the foldmethod to manual when editing starts:

autocmd InsertEnter * if !exists('w:last_fdm') | let w:last_fdm=&foldmethod | setlocal foldmethod=manual | endif

When you're done with editing, reset foldmethod to it's original value:

autocmd InsertLeave,WinLeave * if exists('w:last_fdm') | let &l:foldmethod=w:last_fdm | unlet w:last_fdm | endif
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top