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

How do I close one more level of folds without collapsing all folds at once?

Answer

zm

Explanation

Instead of jumping between fully open (zR) and fully closed (zM), zm and zr let you step through fold levels one at a time. This gives precise control when navigating deeply nested code or structured documents.

How it works

  • zmmore folds: decreases foldlevel by 1, closing one additional nesting level
  • zrreduce folds: increases foldlevel by 1, opening one additional nesting level
  • Both accept a count: 3zm decreases fold level by 3 in one step

Fold level 0 means all folds are closed; higher values open progressively deeper nesting.

Example

In a file with three nesting levels (module → class → method):

▸ module
  ▸ class A
    ▸ method foo
    ▸ method bar
  ▸ class B

Starting with all folds open:

  1. zm → method-level folds collapse (level 3 hidden)
  2. zm → class-level folds also collapse (levels 2–3 hidden)
  3. zr → class-level folds reopen (methods still hidden)

Tips

  • Check or set the current fold level directly: :set foldlevel? / :set foldlevel=2
  • zM and zR are the extremes: close all and open all, respectively
  • Pair zm/zr with zj/zk to navigate between folds at the current level
  • Works with any foldmethod: indent, syntax, marker, manual, or expr

Next

How do I refresh the diff highlighting in Vim when it becomes stale after editing?