How do I save and restore a window's folds, cursor position, and local options?
Answer
:mkview / :loadview
Explanation
:mkview saves the current window's view state — including manual folds, cursor position, scroll position, and local option settings — to a file. :loadview restores it. This lets you close Vim and come back later with your folds and cursor exactly where you left them.
How it works
:mkview— save the current view (folds, cursor, scroll, local options) to a file in&viewdir:loadview— restore the most recently saved view for this file:mkview {N}— save to view slot N (1–9), allowing multiple saved views per file:loadview {N}— restore from slot N- Views are stored in
~/.vim/view/by default (controlled byviewdiroption)
What gets saved
Controlled by the viewoptions option (:set viewoptions?):
- Manual folds and fold state (open/closed)
- Cursor position and scroll offset
- Local options and mappings (
setlocalvalues) - NOT saved by default: window size, tab layout (those are for
:mksession)
Automatic save/restore
Add to your ~/.vimrc to auto-save/load views:
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent! loadview
Now every file automatically remembers its folds and cursor.
Tips
- Use
:mksessioninstead if you want to save the entire Vim session (all windows, tabs, buffers) - Clean up stale view files occasionally:
rm -rf ~/.vim/view/* viewoptionscontrols what's included — removeoptionsfrom it if you only want folds::set viewoptions=folds,cursor- Views are file-specific (keyed by full path), so they work across Vim restarts