How do I open or close folds one level at a time across the entire file?
Answer
zr and zm
Explanation
The zr and zm commands let you incrementally adjust the global fold depth across the entire buffer — one level at a time. Unlike zR (open all) and zM (close all), these commands give you fine-grained control to progressively reveal or hide nested code structure.
How it works
zr— reduce folding: increasesfoldlevelby one, opening the next level of folds across the entire filezm— more folding: decreasesfoldlevelby one, closing the deepest currently-open fold level
Vim's foldlevel setting controls which folds stay open: folds at a level deeper than foldlevel are automatically closed. zr and zm walk this threshold up and down.
Example
Starting from a fully collapsed file (after zM):
+ Module A (folded)
+ Module B (folded)
Press zr to reveal the first level:
- Module A
+ function foo() (folded)
+ function bar() (folded)
- Module B
+ function baz() (folded)
Press zr again to reveal the second level:
- Module A
- function foo()
..body..
- function bar()
..body..
Press zm to collapse back one level.
Tips
- Use
zMfirst to collapse everything, thenzrrepeatedly to explore nested structure layer by layer — ideal for understanding an unfamiliar codebase 2zropens two fold levels at once- The current
foldlevelvalue is visible with:set foldlevel? - Works with any
foldmethod(indent, syntax, marker, expr)