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?
8 Answers
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.
![](../../users/profiles/23840.webp)
- 7,159
- 1
- 27
- 30
-
12For some reason this only partially unfolded the file. I had to use `BufWinEnter` instead. – Kelvin Jul 26 '12 at 18:26
-
just out of curiousity what is the opposite to `zR` – puk May 01 '13 at 03:03
-
3nvm, it's `zM` to close ALL the folds – puk May 01 '13 at 03:04
-
Better with:`if has("autocmd") ... endif` – DrBeco Aug 29 '14 at 05:08
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.
![](../../users/profiles/62699.webp)
- 54,867
- 44
- 156
- 233
-
7but this will lead to a problem: pressing `zm` will not close all folds, unless you enter it 99 times – bitboxer Apr 05 '13 at 07:20
-
19You could also `set nofoldenable`, which temporarily disables folding when you open the file, but all folds are restored as soon as you hit `zc` – 79E09796 May 30 '13 at 08:22
-
1To 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](http://superuser.com/questions/567352)). – Matthew Strawbridge Sep 13 '13 at 21:16
-
4@bitboxer _"but this will lead to a problem: pressing zm will not close all folds"_ Use `zM` to close all folds. – wisbucky Jun 03 '14 at 21:34
-
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. – Brady Trainor Aug 20 '14 at 21:48
-
1
set nofoldenable
Adding this to your .vimrc
will temporarily disable folding when you open the file, but folds can still be restored with zc
![](../../users/profiles/1254222.webp)
- 1,833
- 1
- 17
- 32
-
1Useful - 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. – wom May 11 '18 at 14:30
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
![](../../users/profiles/90580.webp)
- 15,227
- 11
- 82
- 96
-
-
-
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 file – RubenLaguna Oct 05 '18 at 08:14
You can add
set foldlevelstart=99
to your .vimrc file, and it will start editing any new file with all folds open.
![](../../users/profiles/338803.webp)
- 38,979
- 7
- 80
- 102
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.
![](../../users/profiles/3542989.webp)
- 506
- 5
- 19
You can open unfolded file when you put set nofoldenable
into your .vimrc file.
![](../../users/profiles/13480384.webp)
- 26
- 3
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
![](../../users/profiles/591152.webp)
- 100
- 3