Persistent Undo in Vim: Rewind Edits Across Sessions
Keep Vim undo history after quitting: enable undofile, manage the undo directory, and understand the privacy and size trade-offs.
23 Sept 2025, 05:35 UTC

Your undo history disappears when Vim quits
By default, Vim's undo tree lives in memory. When you close the editor, that tree is discarded. If you reopen a file the next day, pressing u only undoes changes made in the current session. You cannot step back to yesterday's saved edits, even though the file on disk still contains them.
The useful takeaway: Vim can write each file's undo tree to disk as a persistent undo file. This survives editor restarts and reboots, but it introduces a directory you must maintain and privacy implications you should review before enabling it globally.
How persistent undo stores history
When undofile is enabled, Vim creates one undo file per edited buffer in a directory controlled by undodir. The file name is hashed from the file's full path so unrelated files do not collide. Unlike a simple change log, the undo file stores the complete undo tree, which means branching undo history—such as undoing, making a new change, then undoing again—survives across sessions.
Vim 7.3 or later is required. Neovim uses the same mechanism but defaults to ~/.local/state/nvim/undo when $XDG_STATE_HOME is set.
Enable it and verify the directory exists
Run these in your shell as your normal user; no root permission is needed for a home directory.
mkdir -p ~/.vim/undodir
Then add to ~/.vimrc:
set undofile set undodir=~/.vim/undodir
For deeper history, optionally raise the limits:
set undolevels=10000 set undoreload=10000
undolevels is how many changes Vim remembers; undoreload is how many changes are kept when you reload a modified buffer.
Inside Vim, confirm the settings with:
:set undofile? undodir?
Expected result: undofile is on and undodir points to the directory you created. If the directory is missing, Vim silently skips writing undo files, so mkdir -p matters.
Worked example: undo yesterday's saved change
- Open a text file:
vim ~/project/config.yaml - Edit a setting, save and quit:
:wq - Reopen Vim the next day:
vim ~/project/config.yaml - Press
urepeatedly. You should step back through edits from the previous session, including changes that were already saved to disk. - Check that undo files were created:
ls -la ~/.vim/undodir. You should see hashed files corresponding to edited buffers.
Risk: if undodir does not exist, step 5 will show an empty directory and cross-session undo will not work without an error message.
Trade-offs and limits
- Silent accumulation. Undo files are never rotated or expired by Vim. A busy project can produce hundreds of megabytes over months.
- Plain-text contents. Each undo file can contain earlier versions of the file. Secrets, PII, or credentials edited in Vim persist under
undodirunless the directory is on an encrypted filesystem or you scrub it manually. - No locking. Multiple Vim instances editing the same file can corrupt its undo file.
- Path dependence. The hashed name is tied to the absolute path. Moving your home directory or the project can orphan existing undo files.
To limit size, exclude large files in ~/.vimrc:
autocmd BufWritePre * if getfsize(expand('%')) > 10*1024*1024 | setlocal noundofile | endif
This disables persistent undo for buffers larger than 10 MiB before they are written.
Practical cleanup and verification
Remove undo files older than 30 days:
find ~/.vim/undodir -type f -mtime +30 -delete
To disable persistent undo entirely, remove set undofile from your config and delete the undodir contents. There is no formal rollback because enabling the feature only writes files; turning it off stops new writes.
Verify the result by editing a scratch file, saving, quitting, reopening, and pressing u. If earlier edits are not reachable, check :set undofile? undodir? and confirm the directory exists and is writable.
Bottom line
Enable persistent undo for text files and configuration where you want cross-session rewind. Keep the undo directory small with scheduled cleanup, avoid it for files containing secrets unless the directory is encrypted, and exclude very large binaries.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.