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
zR— open all folds (mnemonic: Reduce fold level to 0, i.e. open everything). Setsfoldlevelto the depth of the deepest fold.zM— close all folds (mnemonic: Maximize fold level). Setsfoldlevelto 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 recursivelyza— toggle the fold under the cursorzA— 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, usezMto snap everything closed again without manually tracking where folds were zmandzr(lowercase) incrementally decrease or increasefoldlevelby one — useful for partially revealing nested folds- Use
:set foldlevel=1to directly set a specific depth without usingzR/zM