# Vim Session Not Saving

You have multiple files open, windows arranged perfectly, folds set up, and when you close Vim, everything disappears. Session saving preserves your entire Vim state. Here's how to set it up correctly.

Understanding Sessions

Sessions save:

  • Open files and buffers
  • Window layout and splits
  • Tab pages
  • Current directory
  • Arguments
  • Marks
  • Folds
  • Registers (optional)
  • Variables (optional)

Save a Session

vim
:mksession session.vim
" Or abbreviated
:mks session.vim

If the file exists, force overwrite:

vim
:mksession! session.vim

Restore a Session

vim
:source session.vim

Or start Vim with the session:

bash
vim -S session.vim

Configure What Gets Saved

Control session content with sessionoptions:

vim
:set sessionoptions?

Default: blank,buffers,curdir,folds,help,tabpages,winsize

Options:

OptionWhat It Saves
blankEmpty buffers
buffersAll buffers (hidden and visible)
curdirCurrent directory
foldsFold states
globalsGlobal variables (with uppercase letters)
helpHelp windows
localoptionsLocal options and mappings
optionsAll options and mappings
resizeVim window size
skiprtpDon't update runtime path
tabpagesAll tab pages
winsizeWindow sizes

Recommended settings:

vim
set sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,localoptions

Don't Save Too Much

Saving options and globals can cause issues:

```vim " Avoid saving global options set sessionoptions-=options set sessionoptions-=globals

" These can cause conflicts when plugins change ```

Automatic Session Saving

Save on Exit

vim
augroup SessionAutoSave
    autocmd!
    autocmd VimLeavePre * mksession! ~/.vim/session.vim
augroup END

Project-Specific Sessions

Save to project directory:

vim
augroup ProjectSession
    autocmd!
    autocmd VimLeavePre * if getcwd() != $HOME | mksession! .session.vim | endif
augroup END

Automatic Session Loading

vim
augroup SessionAutoLoad
    autocmd!
    autocmd VimEnter * if filereadable('.session.vim') | source .session.vim | endif
augroup END

Or for global session:

vim
augroup SessionAutoLoad
    autocmd!
    autocmd VimEnter * if filereadable('~/.vim/session.vim') | source ~/.vim/session.vim | endif
augroup END

Start Vim Without Loading Session

bash
vim --noplugin

Or from within Vim, don't auto-load:

vim
:let g:session_autoload = 0

Session Directory

Store sessions in a dedicated directory:

vim
let g:session_directory = '~/.vim/sessions'

Create it:

bash
mkdir -p ~/.vim/sessions

Obsession Plugin

The obsession plugin simplifies session management:

vim
Plug 'tpope/vim-obsession'

Run :Obsess to start session tracking. It automatically updates the session file.

ProSession Plugin

For named sessions:

vim
Plug 'dhruvasagar/vim-prosession'
let g:prosession_dir = '~/.vim/sessions'

Use :Prosession name to switch between sessions.

Session Won't Restore Properly

Plugin Issues

Plugins might not restore correctly. Exclude plugin state:

vim
set sessionoptions-=localoptions

Or use skiprtp:

vim
set sessionoptions+=skiprtp

Window Layout Wrong

Window sizes might differ if terminal size changed:

vim
set sessionoptions-=winsize

Buffers Missing

If buffers aren't restored:

vim
set sessionoptions+=buffers

Check if buffer files still exist. Deleted files won't restore.

Folds Not Restored

Folds require fold persistence:

vim
set viewoptions=folds
autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent loadview

Or include in session:

vim
set sessionoptions+=folds

Clear Session

Start fresh:

```vim :only " Close all but current window :tabonly " Close all but current tab :bdelete " Close buffers

:mksession! new_session.vim ```

Neovim Sessions

Neovim sessions work similarly:

lua
vim.opt.sessionoptions = 'blank,buffers,curdir,folds,help,tabpages,winsize'

Use plugins like:

lua
use {
    'tpope/vim-obsession',
    config = function()
        vim.g.obsession_dir = vim.fn.expand('~/.local/share/nvim/sessions')
    end
}

Session File Contents

Session files are Vim scripts. View one:

bash
cat ~/.vim/session.vim

You'll see commands that restore state. Edit it manually if needed.

Multiple Sessions

Named sessions for different projects:

vim
:mksession ~/.vim/sessions/project1.vim
:mksession ~/.vim/sessions/project2.vim

Switch with:

vim
:source ~/.vim/sessions/project1.vim

Git Ignore Sessions

Don't commit session files:

gitignore
.session.vim
Session.vim
*.session.vim

Debug Session Issues

```vim " Show sessionoptions :set sessionoptions?

" Try saving with verbose :verbose mksession test.vim

" Check session file :e session.vim ```

Complete Session Configuration

```vim " Session settings set sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize

" Session directory let g:session_dir = '~/.vim/sessions' if !isdirectory(g:session_dir) call mkdir(g:session_dir, 'p') endif

" Auto-save on exit (optional) augroup SessionSave autocmd! autocmd VimLeavePre * mksession! ~/.vim/sessions/last.vim augroup END

Troubleshooting Checklist

  1. 1.Check sessionoptions: :set sessionoptions?
  2. 2.Verify session file exists: ls ~/.vim/sessions/
  3. 3.Try manual save: :mksession test.vim
  4. 4.Check session file contents: cat test.vim
  5. 5.Verify file paths in session are accessible
  6. 6.Exclude problematic options: sessionoptions-=options
  7. 7.Check for plugin interference
  8. 8.Ensure terminal size matches (for winsize)

Quick Reference

```vim " Save session :mksession name.vim :mks name.vim

" Restore session :source name.vim

" Start with session vim -S name.vim

" Configure options :set sessionoptions=blank,buffers,curdir,folds,tabpages

" Auto-save autocmd VimLeavePre * mksession! ~/.vim/session.vim

" Auto-load autocmd VimEnter * source ~/.vim/session.vim ```

Sessions let you pick up exactly where you left off. Configure them correctly, and your workflow becomes seamless.