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

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 windows
  • curdir — save and restore the current working directory
  • folds — preserve fold state (open/closed) per file
  • globals — save global variables (useful for plugin state)
  • options — save all options and mappings (often a footgun: remove this to let your vimrc stay in charge)
  • tabpages — save all tab pages, not just the current one
  • winsize — 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 options if you find sessions clobbering your theme or plugin settings after restore
  • Add globals when using plugins that store state in global variables (e.g. statusline plugins)
  • Use sesdir instead of curdir to set the working directory to wherever the .vim session file lives — handy for portable project sessions
  • Check current value with :set sessionoptions?

Next

How do I insert the entire current line into the command line without typing it?