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

How do I toggle folding on and off for the entire buffer in one keystroke?

Answer

zi

Explanation

zi toggles the foldenable option for the current window, instantly showing or hiding all folds without altering your fold method or the configured fold levels. It is the fastest way to temporarily see all content in a heavily-folded file — press zi to open everything, read what you need, then press zi again to restore all folds exactly as they were.

How it works

  • z — the prefix for fold-related normal mode commands
  • i — mnemonic for "invert", toggling the boolean foldenable flag
  • When foldenable is off, every fold is displayed as open, regardless of its individual open/closed state
  • Pressing zi again re-enables foldenable, restoring all folds to their previous state — open folds reopen, closed folds re-close

Example

A buffer with syntax-based folding collapses two functions:

▸ def authenticate():                               (22 lines)
▸ def process_request():                            (35 lines)

After pressing zi:

def authenticate():
    if not self.token:
        raise AuthError("missing token")
    ...
def process_request():
    ...

Press zi again and both folds collapse back to their original state.

Tips

  • Unlike zR (which opens all folds without toggling) or zm (which increases fold level), zi is a true toggle — one key for both directions
  • The fold state is per-window, so zi in one split does not affect other splits showing the same buffer
  • Pair with zM to close all folds first, then zi to disable folding entirely for a clean view

Next

How do I sort lines using only the text matched by a regex pattern as the sort key?