How do I save my entire Vim workspace and restore it later?
Answer
:mksession / :source Session.vim
Explanation
Vim's session feature saves a snapshot of your entire workspace — open buffers, window layout, tab pages, current directory, and more — to a file that you can reload later. This means you can close Vim at the end of the day and pick up exactly where you left off tomorrow, with all your splits, tabs, and files intact.
How it works
:mksessionsaves the current session toSession.vimin the current directory:mksession ~/mysession.vimsaves to a specific file path:mksession!overwrites an existing session file:source Session.vimrestores the saved sessionvim -S Session.vimstarts Vim and immediately loads the session from the command line
A session file is just a Vimscript that recreates your workspace. It records:
- All open buffers and their file paths
- Window splits and their sizes
- Tab pages and their layouts
- The current working directory
- Fold states, cursor positions, and local options
Example
You have a complex layout with three tabs, multiple splits, and several files open. At the end of the day:
:mksession! ~/sessions/project-x.vim
Close Vim. The next morning, restore everything in one command:
vim -S ~/sessions/project-x.vim
Vim reopens with every tab, split, buffer, and cursor position exactly as you left it.
Or from inside a running Vim session:
:source ~/sessions/project-x.vim
Tips
- Control what gets saved with the
sessionoptionssetting:
set sessionoptions=buffers,curdir,folds,help,tabpages,winsize
- Remove
optionsfromsessionoptionsto avoid saving global options and mappings — this prevents conflicts with your vimrc when restoring sessions - Create a per-project workflow: always save to
Session.vimin the project root, then start Vim withvim -Sfrom that directory - Map session management for convenience:
nnoremap <leader>ss :mksession! Session.vim<CR>
nnoremap <leader>sl :source Session.vim<CR>
- Use
:mksession!(with bang) to overwrite the existing file — without it, Vim refuses to overwrite and you get an error - Session files do not store buffer contents — only file paths. Unsaved changes are lost when you quit, so save your files before creating a session
- You can maintain multiple session files for different projects or contexts:
~/sessions/frontend.vim,~/sessions/backend.vim, etc. - Neovim and some Vim plugins (like vim-obsession) can auto-save sessions periodically so you never lose your workspace state