How do I save and restore my entire Vim session including open buffers, windows, and layout?
Answer
:mksession
Explanation
:mksession saves a snapshot of the current Vim session — all open buffers, window splits, tab pages, cursor positions, and folds — to a file called Session.vim (or any name you specify). Later you can restore everything with vim -S Session.vim or :source Session.vim.
How it works
:mksession— writes session toSession.vimin the current directory (fails if file already exists):mksession!— overwrites an existing session file:mksession ~/.vim/sessions/project.vim— save to a custom pathvim -S Session.vim— restore the session from the command line:source Session.vim— restore while already inside Vim
The session file is a plain Vim script that replays the buffer list, layout, and settings. You can inspect or edit it manually.
Example
Save before closing:
:mksession! ~/.vim/sessions/myproject.vim
Reopen later:
vim -S ~/.vim/sessions/myproject.vim
Or from inside Vim:
:source ~/.vim/sessions/myproject.vim
Tips
- The
sessionoptionssetting controls what gets saved. Common additions::set sessionoptions+=globals,localoptions - By default,
sessionoptionsincludesblank,buffers,curdir,folds,help,tabpages,winsize,terminal - To save sessions automatically on exit, add to your vimrc:
autocmd VimLeave * mksession! ~/.vim/sessions/last.vim - Pair with a directory-specific session by saving to the project root:
Then open with:mksession! .session.vimvim -S .session.vimfrom that directory