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

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

  1. Install: Plug 'tpope/vim-obsession'
  2. Start tracking: :Obsess (creates Session.vim in the current directory)
  3. Work normally — the session file updates automatically on every change
  4. Quit Vim
  5. 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.vim to your global .gitignore so 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

Next

How do I run a search and replace only within a visually selected region?