# 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
:mksession session.vim
" Or abbreviated
:mks session.vimIf the file exists, force overwrite:
:mksession! session.vimRestore a Session
:source session.vimOr start Vim with the session:
vim -S session.vimConfigure What Gets Saved
Control session content with sessionoptions:
:set sessionoptions?Default: blank,buffers,curdir,folds,help,tabpages,winsize
Options:
| Option | What It Saves |
|---|---|
blank | Empty buffers |
buffers | All buffers (hidden and visible) |
curdir | Current directory |
folds | Fold states |
globals | Global variables (with uppercase letters) |
help | Help windows |
localoptions | Local options and mappings |
options | All options and mappings |
resize | Vim window size |
skiprtp | Don't update runtime path |
tabpages | All tab pages |
winsize | Window sizes |
Recommended settings:
set sessionoptions=blank,buffers,curdir,folds,help,tabpages,winsize,localoptionsDon'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
augroup SessionAutoSave
autocmd!
autocmd VimLeavePre * mksession! ~/.vim/session.vim
augroup ENDProject-Specific Sessions
Save to project directory:
augroup ProjectSession
autocmd!
autocmd VimLeavePre * if getcwd() != $HOME | mksession! .session.vim | endif
augroup ENDAutomatic Session Loading
augroup SessionAutoLoad
autocmd!
autocmd VimEnter * if filereadable('.session.vim') | source .session.vim | endif
augroup ENDOr for global session:
augroup SessionAutoLoad
autocmd!
autocmd VimEnter * if filereadable('~/.vim/session.vim') | source ~/.vim/session.vim | endif
augroup ENDStart Vim Without Loading Session
vim --nopluginOr from within Vim, don't auto-load:
:let g:session_autoload = 0Session Directory
Store sessions in a dedicated directory:
let g:session_directory = '~/.vim/sessions'Create it:
mkdir -p ~/.vim/sessionsObsession Plugin
The obsession plugin simplifies session management:
Plug 'tpope/vim-obsession'Run :Obsess to start session tracking. It automatically updates the session file.
ProSession Plugin
For named sessions:
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:
set sessionoptions-=localoptionsOr use skiprtp:
set sessionoptions+=skiprtpWindow Layout Wrong
Window sizes might differ if terminal size changed:
set sessionoptions-=winsizeBuffers Missing
If buffers aren't restored:
set sessionoptions+=buffersCheck if buffer files still exist. Deleted files won't restore.
Folds Not Restored
Folds require fold persistence:
set viewoptions=folds
autocmd BufWinLeave * mkview
autocmd BufWinEnter * silent loadviewOr include in session:
set sessionoptions+=foldsClear 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:
vim.opt.sessionoptions = 'blank,buffers,curdir,folds,help,tabpages,winsize'Use plugins like:
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:
cat ~/.vim/session.vimYou'll see commands that restore state. Edit it manually if needed.
Multiple Sessions
Named sessions for different projects:
:mksession ~/.vim/sessions/project1.vim
:mksession ~/.vim/sessions/project2.vimSwitch with:
:source ~/.vim/sessions/project1.vimGit Ignore Sessions
Don't commit session files:
.session.vim
Session.vim
*.session.vimDebug 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.Check sessionoptions:
:set sessionoptions? - 2.Verify session file exists:
ls ~/.vim/sessions/ - 3.Try manual save:
:mksession test.vim - 4.Check session file contents:
cat test.vim - 5.Verify file paths in session are accessible
- 6.Exclude problematic options:
sessionoptions-=options - 7.Check for plugin interference
- 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.