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

How do I make mksession restore globals and local options too?

Answer

:set sessionoptions+=globals,localoptions

Explanation

If you rely on sessions for context switching, default :mksession can feel incomplete because some state does not come back. Adding globals and localoptions to sessionoptions makes sessions capture more of your real working environment, not just open buffers and window layout. This is especially useful when your workflow depends on custom variables, per-window behavior, or file-local option tuning.

How it works

:set sessionoptions+=globals,localoptions
  • sessionoptions controls which pieces of state are written into a session file
  • +=globals includes global variables (except internal/readonly ones), so session-aware toggles and workflow flags can be restored
  • +=localoptions saves window- and buffer-local options, such as formatting and display behavior that differ per file or split

After setting this, create a session with :mksession! Session.vim, then restore with :source Session.vim.

Example

Before saving session
- g:review_mode = 1
- One split has a custom local option setup

After reopening with default sessionoptions
- Global flag missing
- Local option tweaks reset

After enabling globals,localoptions and recreating session
- Global flag restored
- Local option tweaks restored

Tips

  • Use this as a baseline, then tune further with additional sessionoptions flags only if needed
  • Put it in your config for consistency across all sessions
  • Recreate existing session files after changing sessionoptions, because old files won’t gain new state retroactively

Next

How do I search for 'bar' that is not preceded by 'foo'?