Using Vim's Persistent Undo to Keep Edit History Across Sessions
Learn how Vim's persistent undo feature saves edit history across sessions, how to enable it, and what trade‑offs to consider.
18 Jan 2026, 02:16 UTC

The problem: undo history vanishes when you quit
When you close Vim after editing a file, the undo stack that lets you press u to step back disappears. If you reopen the same file later, you can only undo changes made during the current session. For workflows that involve frequent saves and reopens—such as iterating on configuration files, reviewing logs, or working with temporary buffers—this forces you to redo work or rely on external version control just to recover a recent edit.
How persistent undo works
Vim can write the undo history to a file on disk when a buffer is written. The feature is controlled by two options:
undofile– when set, Vim creates (or reads) an undo file for each buffer.undodir– specifies the directory where those undo files are stored; the default is the same directory as the edited file, but a central location such as~/.vim/undo-diris often preferred.
When undofile is enabled, Vim automatically loads the undo file when the buffer is opened and appends new changes to it on each write. The undo file is binary; its size grows with the number of changes, not with the size of the source file, so the overhead is usually modest for typical text files.
Setting it up: a concrete example
- Add the following lines to your
~/.vimrc(orinit.vimfor Neovim):set undofile set undodir=~/.vim/undo-dir - Create the directory if it does not exist:
mkdir -p ~/.vim/undo-dir - Restart Vim (or source the vimrc with
:source $MYVIMRC). - Open a file, for example
~/notes.txt, make a few edits, save (:w) and quit (:q). - Reopen the same file (
vim ~/notes.txt) and pressu. You should see the changes from the previous session undone, confirming that the undo history persisted.
Trade‑offs and practical limits
While the feature is lightweight, there are two considerations worth noting for engineering teams:
- Disk usage. Undo files accumulate over time, especially in repositories with many files that are saved frequently. A simple cleanup strategy is to prune files older than a certain age, e.g.,
find ~/.vim/undo-dir -type f -mtime +30 -delete. - Filesystem latency. If the undo directory resides on a slow network share, each write may block while Vim writes the undo file, leading to noticeable save delays. Placing the directory on a local SSD or using a tmpfs mount can mitigate this.
You can verify that the feature is active without affecting your workflow by checking the output of :undolist after a session; it should list numbered entries even after you have closed and reopened the file.
Actionable checklist
- Add
set undofileandset undodir=~/.vim/undo-dirto your vimrc. - Ensure the undo directory is writable and located on fast storage.
- Periodically run a cleanup command to remove old undo files.
- Use
:undolistto confirm persistence after reopening a file. - Monitor save times with
:timeif you suspect latency.
With these steps, your Vim sessions gain a reliable, session‑spanning undo history, reducing the need to rely on external tools for simple edit recovery.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.