How do I open folds one level at a time in Vim without revealing all nested folds at once?
Answer
zr
Explanation
zr ("reduce" fold level) decrements the global foldlevel option by 1, opening the next layer of folds across the entire file. This is the incremental counterpart to zR, which opens all folds at once. With zr, you stay in control — each press reveals one more level of nesting without exposing deeply nested folds you haven't reached yet.
How it works
foldlevelcontrols how many nesting levels are open; a level of 0 means all folds are closedzrruns:set foldlevel+=1(decrements the number of closed levels)zmruns:set foldlevel-=1(the opposite — closes one level)zRopens everything::set foldlevel=99zMcloses everything::set foldlevel=0
Example
With a deeply nested file (say, 4 levels of folds):
+-- 20 lines: Level 1 ---- ← all closed (foldlevel=0)
Press zr → foldlevel=1 ← Level 1 opens
Press zr → foldlevel=2 ← Level 2 opens
Press zr → foldlevel=3 ← Level 3 opens
Press zm → foldlevel=2 ← Level 3 closes again
Tips
- Use
zrto incrementally reveal a large file without getting overwhelmed - Pair with
za(toggle a single fold) for fine-grained control at the cursor - A count prefix works:
2zropens two levels at once - Works with all
foldmethodsettings:indent,expr,marker,syntax,manual