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

VimFile IoDefaultUnfold

Vim Problem Overview


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?

Vim Solutions


Solution 1 - Vim

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.

Solution 2 - Vim

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.

Solution 3 - Vim

set nofoldenable

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

Solution 4 - Vim

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

Solution 5 - Vim

You can add

set foldlevelstart=99

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

Solution 6 - Vim

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.

Solution 7 - Vim

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

Solution 8 - Vim

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuanView Question on Stackoverflow
Solution 1 - VimRookView Answer on Stackoverflow
Solution 2 - VimWalterView Answer on Stackoverflow
Solution 3 - Vim79E09796View Answer on Stackoverflow
Solution 4 - VimRubenLagunaView Answer on Stackoverflow
Solution 5 - Vimsje397View Answer on Stackoverflow
Solution 6 - VimDurga SwaroopView Answer on Stackoverflow
Solution 7 - VimpjobView Answer on Stackoverflow
Solution 8 - VimJunyeong ChoiView Answer on Stackoverflow