Stop Losing Your Undo History: Mastering Vim Persistent Undo
Stop losing your undo history when you close Vim. Learn how to configure persistent undo (undofile) to maintain a durable editing tree across sessions.
09 Oct 2025, 16:46 UTC

The Cost of the Buffer Wipe
By default, Vim's undo history is volatile. The moment you close a file or restart your editor, the undo tree is purged. If you realize an hour after closing a project that a refactor you performed yesterday was a mistake, you cannot simply press u to go back. You are forced to rely on git reverts or manual reconstruction.
Vim solves this with persistent undo. This feature writes the undo history to a disk file, allowing you to revert changes across different sessions, crashes, or system reboots. The takeaway is simple: by configuring a dedicated undo directory, your editing history becomes a durable asset rather than a temporary session state.
Configuring the Undo Archive
To enable persistent undo, you must tell Vim to track undo files and define where those files should live. Without a defined directory, Vim may scatter undo files across your project folders, cluttering your source tree.
Add the following to your .vimrc (or init.vim for Neovim):
set undofile
set undodir=~/.vim/undo//
set undolevels=1000
set undoreload=1000
The undofile setting enables the feature. The undodir setting specifies the storage path. Note the trailing double slash (//) on Unix-like systems; this tells Vim to create a hashed filename based on the absolute path of the file being edited, preventing filename collisions if you edit two different files both named README.md in different directories.
Navigating the Undo Tree
Persistent undo does more than just save history; it preserves the undo tree. In Vim, undoing a change and then making a new change creates a branch. Standard undo (u) and redo (Ctrl-R) only move you along the current branch.
To navigate these branches across sessions, use the following commands:
:undolist: Displays the current branches of the undo tree.g-: Move back in time to the previous change in the tree, regardless of the current branch.g+: Move forward in time.
Worked Example: Verifying Persistence
To verify that your configuration is working correctly, follow this sequence in your terminal:
- Check Support: Run
vim --version | grep +undofile. If you see+undofile, your version supports the feature. - Create a Test: Open a file:
vim test.txt. - Make Changes: Enter Insert mode, type "Hello World", save and exit (
:wq). - Verify Disk Write: Check your undo directory:
ls ~/.vim/undo/. You should see a file with a hashed name. - Test Recovery: Reopen the file:
vim test.txt. Pressu. The text "Hello World" should disappear, even though you started a fresh Vim session.
Trade-offs and Limitations
While durable, persistent undo introduces three specific engineering trade-offs:
- Disk Accumulation: Vim does not automatically delete undo files for files you no longer edit. Over time,
~/.vim/undo/can grow. A practical fix is a periodic cleanup command:find ~/.vim/undo -mtime +30 -delete. - Security: Undo files are plain text and stored in your home directory. They contain fragments of your deleted code. If you work on highly sensitive files on a shared machine, ensure your
undodirpermissions are restricted (chmod 700 ~/.vim/undo). - File Movement: Because the undo file is linked to the absolute path of the source file, renaming or moving a file via the shell (
mv old.txt new.txt) orphans the undo history. The history will not follow the file to its new name.
Final Verification
If you are unsure if a specific file is currently being tracked, run the following command inside Vim:
:echo undofile()
A return value of 1 confirms that persistent undo is active for the current buffer.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.