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

How do I toggle a fold and all its nested folds open or closed at once?

Answer

zA

Explanation

The zA command toggles the fold under the cursor together with all nested sub-folds in one keystroke. Unlike za — which only affects the topmost fold at the cursor — zA recursively opens or closes the entire fold tree beneath the cursor, making it ideal for navigating deeply nested code structures.

How it works

  • If the fold is closed: zA opens it and all nested folds inside it (like zO)
  • If the fold is open: zA closes it and all nested folds inside it (like zC)

This makes zA the recursive counterpart of za:

Command Scope Direction
za Current fold only Toggle
zA Current + all nested Toggle (recursive)
zO Current + all nested Open only
zC Current + all nested Close only

Example

With a nested fold structure:

+-- class Foo ----------+  ← outer fold (open)
|  +-- def bar() ---+  |  ← inner fold (open)
|  |  ...           |  |
|  +----------------+  |
+-----------------------+

Press zA on the outer fold:

+-- class Foo ----------+  ← outer fold (now closed, inner fold hidden)

Press zA again:

+-- class Foo ----------+  ← outer fold (open again)
|  +-- def bar() ---+  |  ← inner fold (open again)

Tips

  • zR opens all folds in the entire file; zM closes all folds — use these for a global reset
  • zm and zr increment or decrement the fold level by one across the whole file
  • Works with any foldmethod (indent, syntax, marker, expr, manual)
  • Combine with a count: 2zA toggles two fold levels at once

Next

How do I highlight all occurrences of a yanked word without typing a search pattern?