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

How do I toggle all folding on and off globally with a single keystroke?

Answer

zi

Explanation

Pressing zi in normal mode toggles the foldenable option, which controls whether folds are active in the current window. When foldenable is off, all folds are displayed as if they are open — regardless of their actual fold level or method. Press zi again to re-enable folding and restore the previous fold state. This is faster than :set foldenable! and is especially useful when you want to temporarily scan an entire file without expanding every fold individually.

How it works

  • z is the prefix for fold-related commands in Vim
  • i toggles foldenable (think: fold inhibit)
  • When foldenable is off, :set nofoldenable is active — all folds are shown open
  • When you press zi again, foldenable is restored and all folds return to their previous state
  • Per-window setting: each split window has its own foldenable state

Example

With a file that has several collapsed folds:

+-- 15 lines: function foo() ---
+-- 23 lines: function bar() ---
baz()

Press zi to instantly see the full file with all folds expanded:

function foo() {
  ...
}
function bar() {
  ...
}
baz()

Press zi again to re-collapse everything as it was.

Tips

  • zR opens all folds (but keeps foldenable on) — unlike zi, folds can still be closed individually
  • zM closes all folds at once
  • za toggles a single fold under the cursor
  • zi is the only command that truly disables the fold mechanism globally for the window

Next

How do I run the same Ex command in every open tab page at once?