Unlocking Vim’s Persistent Undo: How to Keep Your History Across Sessions
Persistent undo in Vim lets you keep undo history across sessions. Enable it with <code>set undofile</code> and <code>set undodir</code>, verify with <code>:undolist</code>, and view the undo tree with a plugin. Learn trade‑offs, cleanup, and a practical example of recovering a lost line.
02 Oct 2025, 01:13 UTC

Why Persistent Undo Matters
When you edit a file in Vim and close it, the default undo history is lost. That means you can’t roll back a change that was made an hour ago, or recover a file after a crash. Persistent undo solves this by storing the undo tree in a separate file that survives Vim restarts. It’s a small configuration change that can dramatically improve your editing workflow.
Getting the Feature Working
Persistent undo was introduced in Vim 7.3. To enable it you need two settings in your ~/.vimrc (or init.vim for Neovim):
set undofile
set undodir=~/.vim/undodir
Explanation of the options:
undofile– turns on the feature.undodir– directory where Vim will write the undo files. The directory must exist and be writable by the user.
When you edit ~/projects/foo.txt, Vim creates a file named ~/.vim/undodir//%7E%2Fprojects%2Ffoo.txt. The tilde and slashes are encoded with a percent sign to make the filename safe on all filesystems.
Verifying the Setup
- Open Vim and edit a file:
vim ~/projects/foo.txt - Make a few edits – insert a line, delete a word, change a sentence.
- Quit Vim:
:q - Re‑open the file and run
:undolist. - The list should show numbered entries, each with a timestamp, and the number of changes should match the edits you made before closing.
At this point you can confirm that an undo file exists in ~/.vim/undodir and its size is greater than zero.
Using the Undo Tree
Vim’s undo history is a tree, not just a linear stack. You can view it with the :undotree command, or use the undotree plugin for a graphical view:
" In your vimrc
Plug 'mbbill/undotree'
" After installing plugins
:UndotreeToggle
The plugin opens a split window showing the branches of the undo tree. You can navigate with j/k and jump to a specific state with u or Ctrl‑R. This is especially useful after a complex series of changes.
Practical Example: Recovering a Lost Line
Suppose you accidentally delete a line in README.md and close the file. With persistent undo you can recover it even after a reboot.
- Open the file again:
vim ~/docs/README.md - Run
:undolist– you’ll see entries like1 2026-09-22 00:03:12and2 2026-09-22 00:04:07. - Press
uuntil you reach the state before the deletion. Eachuundoes one level in the undo tree. - Press
Ctrl‑Rto redo the change if you overshoot. - Save the file. The deleted line is restored.
Because the undo file was written to disk when you closed the file the first time, the history was preserved across the entire session.
Trade‑offs and Limitations
- Disk Usage: Each undo file grows with the number of undo entries. For very large files or files that change a lot, the file can become several megabytes. You can monitor the size with
ls -lh ~/.vim/undodir/*. - Performance: Writing to a network or very slow filesystem can add latency to
:wandq. If you notice delays, consider movingundodirto a local SSD. - Compatibility: Undo files created by Vim 7.3+ are not readable by older versions. Keep the
undodirprivate to the Vim installation that created it. - Security: Undo files contain the raw text of your edits, including potentially sensitive data. Protect the
undodirwith appropriate filesystem permissions (e.g.,chmod 700 ~/.vim/undodir).
Cleaning Up
When you no longer need the undo history for a file, you can delete its undo file:
rm ~/.vim/undodir//%7E%2Fprojects%2Ffoo.txt
Or prune old files with a simple find command:
find ~/.vim/undodir -type f -mtime +30 -delete
That removes undo files older than 30 days.
Actionable Checklist
- Ensure Vim is version 7.3 or newer.
- Add
set undofileandset undodir=~/.vim/undodirto yourvimrc. - Create the undodir directory and set
chmod 700for privacy. - Restart Vim, edit a file, and verify
:undolistpersists after closing. - Consider installing the
undotreeplugin for visual navigation. - Monitor disk usage and clean up old undo files periodically.
Once you have persistent undo in place, you’ll find that accidental edits become less scary, and you can experiment more freely knowing you can always go back.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.