Keep Vim Undo History After You Quit: Enable Persistent Undo
Enable Vim's undofile option so undo history survives editor restarts, with verification steps and cleanup guidance.
10 Sept 2026, 17:49 UTC

The problem: undo history disappears when Vim exits
By default, Vim keeps undo history only in memory. When you quit, that history is discarded. If you reopen a file later and want to undo a change from the previous session, pressing u has no effect.
Useful takeaway: enable Vim's persistent undo feature so Vim writes undo history to files on disk. You can then undo changes from earlier sessions after restarting the editor.
Prerequisites
- Vim 7.0 or later. Persistent undo was introduced in Vim 7.0.
- A writable location for undo files, such as a directory under your home folder.
- Permission to edit your Vim configuration file, usually
~/.vimrc.
Enable persistent undo
Create the undo directory. Run this in your shell, not inside Vim:
mkdir -p ~/.vim/undo
The -p flag creates parent directories if needed. Vim may not create missing parent directories for undodir, so create it manually before editing.
Add these lines to ~/.vimrc:
set undofile
set undodir=~/.vim/undo//
undofile tells Vim to serialize undo history to disk. undodir is the directory where those undo files are stored. The trailing double slash (//) is important: Vim encodes the full path of the edited file into the undo filename, which prevents collisions when different files have the same basename.
Restart Vim, or reload the configuration in your current session:
:source $MYVIMRC
Run this command inside Vim in Normal mode. It requires no special filesystem permissions beyond reading your vimrc.
Verify persistent undo is active
Check the option values inside Vim:
:set undofile?
:set undodir?
These commands confirm undofile is enabled and undodir points to the directory you created. If Vim cannot write to undodir, it silently falls back to in-memory undo only, so verify the directory exists and is writable.
Now run a functional check:
- Open a file:
vim ~/example-persistent-undo.txt - Enter Insert mode, type some text, save and exit:
:wq - Reopen the same file:
vim ~/example-persistent-undo.txt - Run
:undolist. The edit from the previous session should appear in the list. - Press
u. The text you added before quitting should disappear.
undolist displays numbered undo branches stored for the current file. u reverses the most recent change, including changes loaded from a persistent undo file.
Recover an older state after restart
After reopening a file with persistent undo enabled, you can navigate the undo tree:
u: undo the last change.Ctrl-R: redo a change you just undid.:undo N: jump to undo branch numberNfrom:undolist.
Persistent undo is keyed to the edited file's full path. If you rename or move the file, Vim cannot find its undo file, and the history is effectively orphaned.
Limitations and cleanup
- Undo files may contain sensitive text from edited buffers. Restrict permissions on the undo directory by running
chmod 700 ~/.vim/undoin your shell. - Do not place
undodirinside a Git or other version-control repository unless you intend to track undo files. - Undo files grow with use. Prune the directory occasionally or reduce
undolevelsin your vimrc to limit how many changes Vim remembers. - If
:set undofile?returnsnoundofile, the option is not active and Vim is not writing undo files.
Rollback
This procedure changes filesystem state by creating a directory and editing your vimrc. To reverse it:
- Remove the two
setlines from~/.vimrc. - Optionally delete the undo directory:
rm -rf ~/.vim/undo. This permanently discards all stored undo history.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.