Persisting Undo in Vim: Enable, Store, and Navigate Your Edit History
Enable Vim’s persistent undo, understand where undo files are stored, learn how to navigate the undo tree, and discover practical tips for managing and cleaning up undo data.
18 Aug 2026, 23:00 UTC

Why Persistent Undo Matters
When you edit a file, Vim keeps a stack of changes in memory. If the editor closes or crashes, that history disappears. Persistent undo writes the entire change tree to disk so you can return to any earlier point even after a session ends.
Enabling the Feature
Two options control this behavior:
set undofile " Write undo data to a file
set undodir=~/.vim/undo " Optional directory for those files
Add these lines to ~/.vimrc (or init.vim for Neovim) and restart. After startup, verify with:
:set undofile?
:set undodir?
If undodir is empty, Vim will create the undo file in the same directory as the edited file. The undodir option only takes effect when it is non‑empty.
Where Undo Files Go
When undodir is set, Vim creates a subdirectory under that path. The file name is derived from the absolute file path but with path separators replaced by % and a .un~ suffix. For example, editing /home/alice/project/readme.md will produce a file named
~/.vim/undo/%home%alice%project%readme.md.un~.
If the directory is not writable, Vim prints an error such as E510: Cannot make undo file and falls back to in‑memory undo. It does not silently ignore the problem.
Navigation Inside the Undo Tree
Once persistent undo is active, you can use the usual u and Ctrl‑r keys, plus a few additional commands:
:undo/:redo– linear undo/redo.:earlier {count|time}– jump back to a specific number of changes or a time stamp.:later {count|time}– move forward.g-/g+– normal‑mode shortcuts for:earlierand:later.:undolist– list all undo entries with timestamps.
Worked Example
- Open
example.txtin Vim. - Make a few edits. After each change, you can run
:undolistto see the new entries. - Close Vim.
- Reopen
example.txtand run:undolistagain. The same entries should appear, proving persistence. - Move back 10 seconds:
:earlier 10s. The buffer should reflect the state from that time. - Redo one step:
:redo 1or simplyg+.
Practical Considerations
- Disk Space: Undo files can grow with the number of changes. Use
set undolevels=1000to cap the size if you edit very large files. - Security: Undo files are plain binary blobs and are not encrypted. Sensitive data may remain on disk until the file is removed.
- Compatibility: The binary format has been stable since Vim 7.3, so files written by newer Vim can be read by older Vim that supports the same format.
- Performance: Writing undo data on every change may add a slight overhead. If you notice lag, consider disabling
undofilefor very large files or temporarily clearing the undo file with:wundo!.
Cleaning Up Old Undo Files
When you finish a project, you can prune old undo files to reclaim space:
find ~/.vim/undo -type f -mtime +30 -delete
This command deletes undo files older than 30 days. Adjust the threshold as needed.
Conclusion
Persistent undo turns a transient editor into a durable workspace. By setting undofile and, optionally, undodir, you gain the ability to recover from accidental edits, crashes, or accidental quits. The trade‑offs—extra disk usage and a separate directory to manage—are minor compared to the safety net it provides. Add the two lines to your vimrc, verify with :set undofile?, and start editing with confidence.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.