vimtricks.wiki Concise Vim tricks, one at a time.

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

  • zrreduce folding: increases foldlevel by one, opening the next level of folds across the entire file
  • zmmore folding: decreases foldlevel by 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 zM first to collapse everything, then zr repeatedly to explore nested structure layer by layer — ideal for understanding an unfamiliar codebase
  • 2zr opens two fold levels at once
  • The current foldlevel value is visible with :set foldlevel?
  • Works with any foldmethod (indent, syntax, marker, expr)

Next

What built-in LSP keymaps does Neovim 0.10+ provide automatically when a language server is attached?