Making Vim Safer with Persistent Undo Across Sessions
Learn how Vim’s undofile feature lets you revert to any earlier edit even after you quit, plus how to enable it safely and what trade‑offs to watch for.
24 Mar 2026, 20:52 UTC

The problem: losing history when you quit Vim
When you make a series of edits in Vim and then exit without saving, the undo stack disappears. If you later realize you need a change from an earlier session, you must rely on external backups or version control. This makes quick experimentation risky.
Thesis: persistent undo turns Vim into a safer workspace
By enabling the undofile option, Vim writes each buffer’s undo history to a file on disk. When you reopen the same file, the undo tree is restored, letting you step back to any previous state—even after quitting.
How persistent undo works
Vim maintains an undo tree where each node represents a buffer state and edges represent edits. With undofile set, the tree is serialized to a file named after the edited file (often with a trailing comma or Unicode marker). The file stores:
- All undo branches, so you can revert to a sibling change.
- Timestamps that allow commands like
:earlier 10mto roll back to a point ten minutes ago. - Metadata needed to rebuild the tree on the next load.
The undo file is independent of the swap file; it survives crashes and does not affect recovery.
Practical setup: adding the options to your vimrc
Add the following lines to ~/.vimrc (or init.vim for Neovim):
" Enable persistent undo
set undofile
" Choose a directory for undo files; ensure you have write permission
set undodir=~/.vim/undo
" Create the directory if it does not exist
if !isdigit(expand('~/.vim/undo'))
call mkdir(expand('~/.vim/undo'), 'p', 0700)
endif
Explanation:
set undofileactivates the feature.set undodirtells Vim where to store the undo files; a dedicated directory keeps your home tidy.- The
mkdircall ensures the directory exists with restrictive permissions (read/write for you only).
After saving your vimrc, restart Vim or source the file with :source $MYVIMRC.
Worked example: editing, quitting, and retrieving past changes
- Open a test file:
vim ~/test-persistent-undo.txt. - Make a few changes, saving after each with
:w(or let Vim autosave if you haveautowrite). - Quit Vim:
:q. - Reopen the same file.
- Run
:undolistto see a numbered list of stored undo entries, each with a timestamp. - To go back ten minutes:
:earlier 10m. - To step through individual changes: repeatedly press
uor use:undo 1,:undo 2, etc.
You should observe the buffer revert to the state you selected, confirming that the undo tree survived the session break.
Trade‑off and limitation: disk usage and external changes
Persistent undo trades safety for storage:
- Each undo file grows with the number and size of edits. Large files (e.g., logs) can produce undo files many times larger than the original.
- Startup may feel slower as Vim reads the undo file, though the impact is usually modest unless the file is huge.
- The feature only protects against changes made inside Vim. If another process modifies the file while Vim is not running, the undo tree becomes out of sync and Vim will warn you on reload.
To monitor size, run after a session:
:!ls -lh ~/.vim/undo/$(fnameescape(expand('%:t'))),~
Replace the path with your chosen undodir. If the file is excessively large, consider:
- Setting
ulimitundoorundoreloadto limit how many undo levels are saved. - Periodically cleaning old undo files with a cron job that removes files older than a chosen threshold.
Actionable close: test, verify, then adopt
1. Create a temporary undo directory: mkdir -p ~/.vim/undo-test.
2. Add set undofile and set undodir=~/.vim/undo-test to your vimrc.
3. Open a file, make several edits, quit, and reopen.
4. Verify with :undolist and try :earlier 5s or :undo.
5. Check the undo file size with the ls -lh command above.
6. If the behavior meets your needs and the size is acceptable, move the settings to your permanent vimrc and point undodir to a long‑term location (e.g., ~/.vim/undo).
7. Remember to exclude the undo directory from version control (add it to .gitignore) to avoid committing large undo files.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.