How do I automatically save and restore my Vim session including windows, tabs, and buffers?
Answer
:Obsess / vim -S Session.vim
Explanation
vim-obsession (by Tim Pope) enhances Vim's built-in :mksession by continuously saving the session file as you work. Once started with :Obsess, every window split, tab open, buffer change, and directory change is automatically recorded. When you reopen Vim with vim -S, your entire workspace is restored exactly as you left it.
How it works
- Install:
Plug 'tpope/vim-obsession' - Start tracking:
:Obsess(createsSession.vimin the current directory) - Work normally — the session file updates automatically on every change
- Quit Vim
- Restore:
vim -S Session.vim— everything comes back
Commands
| Command | Action |
|---|---|
:Obsess |
Start tracking to Session.vim |
:Obsess {file} |
Start tracking to a custom filename |
:Obsess! |
Stop tracking and delete the session file |
vim -S |
Restore from Session.vim |
vim -S {file} |
Restore from a specific session file |
What gets saved
- Window layout (splits, sizes)
- Tab pages
- Current working directory
- Buffer list and cursor positions
- Fold state
Why obsession over :mksession?
Vim's built-in :mksession only saves a snapshot at the moment you run it. If you forget to run it before quitting, your layout is lost. Obsession saves continuously — you never have to remember.
Tips
- Add
Session.vimto your global.gitignoreso session files don't get committed - Combine with vim-prosession for automatic per-directory sessions
- The session file is just Vimscript — you can edit it if needed
- Check tracking status with
:echo ObsessionStatus()— useful for statusline integration