How do I automatically save and restore fold states between Vim sessions using autocmds?
autocmd BufWinLeave * mkview
By default, Vim forgets your folds, cursor position, and scroll state every time you close a file.
autocmd BufWinLeave * mkview
By default, Vim forgets your folds, cursor position, and scroll state every time you close a file.
zfap
Combining the fold-creation operator zf with the ap (around paragraph) text object gives you a quick way to collapse any paragraph into a single fold line.
:lua require('ufo').openAllFolds()
nvim-ufo is a Neovim plugin that replaces the built-in fold system with one powered by LSP (textDocument/foldingRange) or treesitter.
zO and zC
zO and zC are the recursive counterparts to zo and zc.
set foldmarker=start,end
When using foldmethod=marker, Vim uses {{{ and }}} as the default fold open and close markers.
zj
When your file uses folds, zj and zk let you navigate directly to fold boundaries — jumping to the start of the next fold below or the end of the previous fol
:set foldmarker=region,endregion
When using foldmethod=marker, Vim looks for the strings in foldmarker to identify fold boundaries.
:set foldcolumn=3
The foldcolumn option adds a narrow column on the left side of the window that visually represents the fold structure of the file.
zr and zm
The zr and zm commands let you incrementally adjust the global fold depth across the entire buffer — one level at a time.
zm
Instead of jumping between fully open (zR) and fully closed (zM), zm and zr let you step through fold levels one at a time.
zR and zM
When working with a heavily folded file, manually opening or closing each fold is tedious.
zd
zd removes the fold definition at the cursor position — the text inside the fold is not deleted.
zO
zO (uppercase O) opens the fold under the cursor and all folds nested inside it recursively.
[z and ]z
The [z and ]z commands jump the cursor to the start and end of the innermost open fold containing the cursor.
zi
Pressing zi in normal mode toggles the foldenable option, which controls whether folds are active in the current window.
zx
The zx command resets and recomputes all folds in the current window based on the current foldmethod.
zf{motion}
Vim supports several fold methods, but manual folding with zf gives you precise control over exactly which lines to collapse.
zf (in visual mode)
In visual mode, pressing zf creates a manual fold from the selected lines.
zj and zk
How it works When working with folded code in Vim, you often want to skip from one fold to another without unfolding anything.
:set foldmethod=marker
Setting foldmethod=marker lets you define fold boundaries using special comment markers — {{{ to start a fold and }}} to end it.