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?
Asked
Active
Viewed 1,435 times
14
-
I can't reproduce this problem with a default vim installation and a simple Java file... – krakover Jan 08 '11 at 11:09
-
I also can't reproduce this. Curious, do you have `showmatch` turned on? Does this setting affect the behavior? – Randy Morris Jan 08 '11 at 16:42
-
@r.v I wonder what do you have in your `foldmarker`? – ZyX Jan 08 '11 at 22:56
-
@krakover, @Randy Morris I believe that you can reproduce this behavior with default vim setup if you will do the following: start vim with `vim -u NONE -c 'set nocompatible fdm=marker fmr={,}' -c "call setline('.', repeat(['{', '}'], 3))"` and then typing `zoo{`. – ZyX Jan 08 '11 at 23:03
-
@Randy Morris `showmatch` is off. @ZyX I am using `foldmethod=syntax`. `fmr={{{,}}}` but I think it is not used here. – r.v Jan 10 '11 at 17:50
-
@r.v `fmr` - yes, it is not used. But cause is just the same: when you type `{` you make all blocks below be contained in the current block. As current block is open, all new contained blocks appear open too. I don't know any workarounds. Personally I use `fdm=marker` and use only markers of the form `{{{N` (and, sometimes, with closing `}}}N`). In case you specify foldlevel explicitely with `N` it does not autoopen folds. – ZyX Jan 10 '11 at 19:20
1 Answers
21
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

eckes
- 64,417
- 29
- 168
- 201