Keep Vim Undo History Across Sessions with Persistent Undo Files
Vim discards undo history when you quit by default. Here is how to enable persistent undo, where to store the files, and what to watch out for.
07 Jun 2026, 04:07 UTC

You close Vim, reopen the same file, press u, and nothing happens. The change you made ten minutes ago is gone from the undo chain. By default, Vim keeps undo history in memory only; when the editing session ends, that history is discarded. If you want undo to survive restarts, you need persistent undo.
undofile, point undodir at a dedicated folder, and treat the resulting files as ephemeral editor state rather than project artifacts.
Why Vim Forgets Your Undo History
Vim's undo tree lives in memory while a buffer is open. When you quit or close the buffer, Vim releases that tree unless persistent undo is active. Many graphical editors maintain a longer, file-backed undo chain by default, so the transition to Vim can feel like a regression. The feature exists, but it is off unless you turn it on.
Turning On Persistent Undo
Add two settings to your .vimrc:
set undofile
set undodir=~/.vim/undodir
undofile tells Vim to write the undo tree to disk. undodir chooses where those files live. Centralizing them in one directory keeps your project folders clean and makes cleanup easier.
Create the directory before restarting Vim. In your shell, run:
mkdir -p ~/.vim/undodir
Use permissions appropriate for a single-user machine. On shared systems, restrict access because undo files can contain buffer text. If the directory does not exist, Vim may fail to save undo files or fall back to a default location depending on version and platform, so create the path first.
Worked Example and Verification
First confirm the feature is compiled in. In Vim command mode, run:
:echo has('persistent_undo')
It should return 1. If it returns 0, your Vim build lacks the feature and configuration alone will not help.
Next verify the options are active:
:set undofile?
:set undodir?
You should see undofile and the path you configured.
Now test persistence:
- Open a text file and make a visible change.
- Save with
:wand quit with:q. - Reopen the same file.
- Press
uin normal mode.
The change from the previous session should revert. You can also traverse the persisted tree with :earlier and :later.
As a file-system check, list your undo directory after the save-and-reopen cycle:
ls ~/.vim/undodir
A new file should appear. Vim names these files using an encoded form of the absolute path, so the name will not match your original filename exactly.
Trade-offs, Limitations, and Rollback
Persistent undo is not free. The undo tree consumes disk space, and large files with extensive editing can generate sizable undo files.
Privacy is the sharper concern. Undo files store buffer content, including text you may have deleted. Treat them like swap files: audit them on shared machines and consider a cleanup policy. Do not assume they are harmless metadata.
Staleness is another limitation. If a file is edited outside Vim or by concurrent sessions, the persisted undo tree may no longer align cleanly with the buffer. Vim will still load the tree, but the sequence of states may be confusing.
Vim and Neovim can differ in default undo paths and file naming. Verify behavior in the editor your team actually uses rather than assuming the configuration is identical.
Persistent undo complements but does not replace version control or frequent saves. It is a safety net for editing sessions, not a history system.
To make this operational, treat undo files as ephemeral editor state. Keep undodir outside project repositories, and if you ever place it inside one, add it to .gitignore immediately. Exclude the directory from backups if your backup policy already covers editor state elsewhere.
If you need to roll back the change, disable the option in your .vimrc:
set noundofile
Existing undo files remain on disk until you remove them. Delete the directory contents only after confirming you no longer need that history. Enable persistent undo, centralize the files, and verify the feature works in your build. It is a small configuration change that removes a common friction point when moving between editing sessions.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.