I use foldmethod=indent
but I hate how it starts each file pre-folded. To get around this I put au BufWinEnter * normal zi
in my vimrc. That had worked fine so far, but now that I started using CoC, My error messages are getting auto-folded, which is obviously not what I want. I tried adding MenuPopup
to the autocommand but no luck. How can I get this... window? (Idk what it is as far as Vim is concerned) to no automatically fold its text?
-
If the problem is that the files open pre-folded you can try this: How to save a view (with folds) into the file?– edugomez102Commented Jan 17, 2020 at 15:02
2 Answers
Put set nofoldenable
in your vimrc. This will also accomplish what your autocommand does.
-
1This does indeed keep everything unfolded initially, but as soon as you toggle on one fold, that changes the value of
nofoldenable
and the entire file gets folded again. That might be sufficient for certain use cases but not others. Commented Oct 2, 2023 at 17:50
Although this is probably the best place for this question, it has also been asked on other more popular sites:
- https://superuser.com/q/278442/9458
- https://superuser.com/q/169973/9458
- https://stackoverflow.com/q/8316139/120999
Answers generally fall into three categories.
The first is what has already been posted here, which is to set nofoldenable
. This on first glance appears to be correct, but it's an internal variable modified by the various fold commands. The consequence of this is that as soon as you execute a fold command (for instance, za
), nofoldenable
is overwritten and all folds are collapsed. This is probably not what you want.
The second is to use BufRead
or similar to execute a folding command once you open a buffer. This will work, but to my mind is more of a hack than option three.
Option three then being to utilize foldlevelstart
. Foldlevel is a variable that
Sets the fold level: Folds with a higher level will be closed. Setting this option to zero will close all folds. Higher numbers will close fewer folds. This option is set by commands like zm, zM and zR.
(Note that it isn't modified by smaller-scoped fold commands like za
, like nofoldenable
is.)
foldlevelstart
then sets foldlevel
when opening a buffer:
Sets 'foldlevel' when starting to edit another buffer in a window. Useful to always start editing with all folds closed (value zero), some folds closed (one) or no folds closed (99).
This is all to say that you can use a snippet like this in your vimrc:
" Enable folding, but don't collapse folds by default.
set foldmethod=indent
set foldlevelstart=99