How do I control exactly what Vim saves when I create a session with :mksession?
Answer
:set sessionoptions
Explanation
The sessionoptions option (abbreviated ssop) is a comma-separated list of flags that determine what :mksession stores in the session file. By default it saves far more than most workflows need — and critically, the options flag causes the session to override your vimrc settings on restore. Tuning it gives you lightweight, predictable sessions.
How it works
Key flags and what they control:
buffers— save all open buffers (including hidden ones), not just the ones in windowscurdir— save and restore the current working directoryfolds— preserve fold state (open/closed) per fileglobals— save global variables (useful for plugin state)options— save all options and mappings (often a footgun: remove this to let yourvimrcstay in charge)tabpages— save all tab pages, not just the current onewinsize— save the sizes of all windows
Example
A lean session that restores tab layout, buffers, folds, and working directory — without overriding your vimrc:
" In your vimrc
set sessionoptions=buffers,curdir,folds,tabpages,winsize
After adjusting sessionoptions, generate and restore sessions as usual:
:mksession ~/.vim/sessions/project.vim
:source ~/.vim/sessions/project.vim
Tips
- Remove
optionsif you find sessions clobbering your theme or plugin settings after restore - Add
globalswhen using plugins that store state in global variables (e.g. statusline plugins) - Use
sesdirinstead ofcurdirto set the working directory to wherever the.vimsession file lives — handy for portable project sessions - Check current value with
:set sessionoptions?