How to set the default to unfolded when you open a file?
Asked Answered
N

9

93

In my .vimrc I've put set foldmethod=syntax to enable folding of methods etc. However, I don't like the default that everytime I open a file, the whole thing is folded. Is there a way to enable foldmethod, yet have files unfolded when I open them?

Nagpur answered 29/11, 2011 at 18:54 Comment(1)
I found this article helpful to pick the right answer.Irresoluble
S
80
set foldlevel=99

should open all folds, regardless of method used for folding. With foldlevel=0 all folded, foldlevel=1 only somes, ... higher numbers will close fewer folds.

Seminary answered 29/11, 2011 at 19:22 Comment(7)
but this will lead to a problem: pressing zm will not close all folds, unless you enter it 99 timesTojo
You could also set nofoldenable, which temporarily disables folding when you open the file, but all folds are restored as soon as you hit zcLollar
To set the exact foldlevel you can use :autocmd BufWinEnter * let &foldlevel = max(map(range(1, line('$')), 'foldlevel(v:val)')) (taken from an answer on superuser).Sift
@Tojo "but this will lead to a problem: pressing zm will not close all folds" Use zM to close all folds.Mercia
To expand on wisbucky's reply, 99 is not a problem since zM indeed completely corrects its effects. See :help foldlevel and :help zm. foldlevel=0 is a buffer value, defaulting to 0, and easily reset, as witbucky points out, with zM. To add to 79E09796's great idea, zi is also nice, inverting foldenable, which remembers your fold choices.Shalne
@Lollar You should move your comment to an answer of its own.Briant
For anyone using Lua to configure: vim.wo.foldlevel = 99Diphthong
N
71

You can put this in your .vimrc: au BufRead * normal zR

It declares an automatic command (au), triggered when a buffer is read (BufRead), matching all files (*) and executes the zR (opens all folds) command in normal mode.

Noella answered 29/11, 2011 at 19:50 Comment(4)
For some reason this only partially unfolded the file. I had to use BufWinEnter instead.Tapia
just out of curiousity what is the opposite to zRSoubise
nvm, it's zM to close ALL the foldsSoubise
Better with:if has("autocmd") ... endifHippogriff
L
33
set nofoldenable

Adding this to your .vimrc will temporarily disable folding when you open the file, but folds can still be restored with zc

Lollar answered 31/5, 2016 at 10:19 Comment(1)
Useful - but when you zc a fold it then hides all other folds also all-at-once. I think i like personally defaulting to foldlevel=99 as it keeps 'zc' then localized to individual chunk you are looking at when invoked.Beadledom
I
13

In .vimrc add an autocmd for BufWinEnter to open all folds automatically like this:

autocmd BufWinEnter * silent! :%foldopen!

That tell vim to execute the silent :%foldopen! after opening BunWinEnter event (see :h BufWinEnter). The silent %foldopen! will execute foldopen on the whole buffer thanks to the % and will open all folds recursively because of the !. Any eventual error message will be suppressed by silent. (You could get error messages like E490: No fold found if the buffer actually didn't contain any fold yet)

Note: You could use BufRead instead of BufWinEnter but then if the file has a modeline that enables the folding that will override this autocmd. I mean BufRead autocmds run before the modeline is processed and BufWinEnter will run them after. I find the later to be more useful

Ilene answered 15/5, 2014 at 7:58 Comment(3)
Why not just use a modeline that set different fold options per file?Dulcy
using autocmd here allows to open all folds for all files using a modeline it just to apply to that file and assumes that you can modify the file (it could be a read only file). The modeline would look like # vim: set foldlevel=99 at the top or bottom of the fileIlene
How can this be achieved in Lua?Primrosa
B
10

You can add

set foldlevelstart=99

to your .vimrc file, and it will start editing any new file with all folds open.

Byington answered 28/9, 2014 at 8:15 Comment(1)
This should be the accepted answer because it is an actual dedicated setting in vim that does exactly what the question is asking for.Speight
E
4

If you want a way to have it display unfolded as soon as it is opened, you can use set foldlevelstart=99 as a lot of answers explained.

But, if you just want to see them unfolded, you can just press zi and it will unfold everything. Another, zi will close them back.

Elly answered 17/12, 2015 at 12:12 Comment(0)
M
0

You could map it to keys to enable it. For example,

nmap ,f :set foldmethod=syntax<CR>

Then while in normal mode hit the ",f" key combination

Merchantable answered 29/11, 2011 at 19:21 Comment(0)
T
0

You can open unfolded file when you put set nofoldenable into your .vimrc file.

Tumescent answered 13/7, 2020 at 3:12 Comment(0)
S
0

autocmd BufReadPost * silent! :%foldopen!

This worked best for me. After a buffer gets opened all folds are opened. This opens them to the correct level.

The set foldenable method was not good, because if I choose to close one fold level, it enabled folding again, and folded every thing to 0 level, instead of just going down one level on the one I activated.

Sport answered 23/10, 2022 at 9:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.