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

How do I open or close every fold in the entire file at once?

Answer

zR and zM

Explanation

When working with a heavily folded file, manually opening or closing each fold is tedious. zR opens every fold in the file instantly by raising foldlevel to its maximum, while zM closes every fold by setting foldlevel to zero. Together, they give you one-keystroke control over the global fold state.

How it works

  • zRopen all folds (mnemonic: Reduce fold level to 0, i.e. open everything). Sets foldlevel to the depth of the deepest fold.
  • zMclose all folds (mnemonic: Maximize fold level). Sets foldlevel to 0, collapsing everything.
  • Both operate on the entire buffer, not just the fold under the cursor.

For finer control:

  • zo / zc — open / close the fold under the cursor (one level)
  • zO / zC — open / close the fold under the cursor recursively
  • za — toggle the fold under the cursor
  • zA — toggle recursively

Example

Starting state (folded):

+-- 12 lines: function parseConfig() ---
+--  9 lines: function renderView() ---
+--  5 lines: function cleanup() ------

After pressing zR:

function parseConfig() {
  const cfg = readFile()
  ...
}
function renderView() {
  ...
}
function cleanup() {
  ...
}

Press zM to collapse all folds back to the starting state.

Tips

  • After zR, use zM to snap everything closed again without manually tracking where folds were
  • zm and zr (lowercase) incrementally decrease or increase foldlevel by one — useful for partially revealing nested folds
  • Use :set foldlevel=1 to directly set a specific depth without using zR/zM

Next

How do I permanently add a word to my personal spell file so Vim stops marking it as misspelled?