Making Vim Remember Undo Across Sessions with Persistent Undo
Learn how to enable Vim's undofile feature, configure a dedicated undo directory, and verify that your edit history survives file closes and reopens.
22 Nov 2025, 04:51 UTC

The problem: losing undo history when you close a file
When you edit a file in Vim, make a few changes, then exit with :wq, the undo stack disappears. Reopening the same file later means you can only undo changes made in the current session. If you need to revert to an earlier version from yesterday, you have to rely on external version control or manual backups.
Thesis: Vim’s built‑in persistent undo (undofile) solves this by storing undo data on disk
Starting with Vim 7.0, the undofile option tells Vim to write a separate undo file each time a buffer is written. That file contains the complete undo branch history, so you can undo or redo edits even after Vim has been restarted. The feature works on Linux, macOS, and Windows, and requires no external plugins.
Enabling and configuring persistent undo
Add the following lines to your
~/.vimrc(orinit.vimfor Neovim):set undofile " enable persistent undo set undodir=~/.vim/undo" directory where undo files will be storedIf the directory does not exist, Vim will create it automatically when the first undo file is needed.
Optionally control how much undo history is kept:
set undolevels=1000 " maximum number of changes that can be undone set undoreload=10000 " maximum number of lines to save for undo on a buffer reloadThese values are measured in changes and lines, respectively. Adjust them based on your typical editing volume and available disk space.
Worked example: verifying that undo survives a session
Open a test file:
vim ~/tmp/persistent-undo-test.txtMake a few edits, for instance:
- Insert the line "first change" and save (
:w). - Insert "second change" and save again.
- Insert "third change" and save.
- Insert the line "first change" and save (
Quit Vim (
:q). At this point Vim has written an undo file to~/.vim/undo. You can list its contents:ls -l ~/.vim/undoYou should see a file whose name is derived from the original path, often something like
%home%user%tmp%persistent-undo-test.txt.un~or a hash‑based name.Reopen the same file:
vim ~/tmp/persistent-undo-test.txtNow try to undo beyond the current session’s changes. For example, go to the end of the file and type:
:earlier 2fThis command tells Vim to go back two "file‑write" events. You should observe the buffer reverting to the state after the first change, confirming that the undo history persisted across the quit/reopen cycle.
To inspect the undo branches stored in the persistent file, run:
:undolistThe output will list numbered changes with timestamps, showing that Vim retained the earlier edits.
Trade‑offs and limitations
Disk usage: each undo file grows with the number of changes you make. Heavy editing sessions can produce undo files that are larger than the original text. Periodically cleaning the
undodir(e.g., deleting files older than a month) or loweringundolevelshelps control space.Privacy: because undo files retain the exact text you edited, they may contain sensitive information. If you work on confidential files, consider placing
undodirin a directory with restrictive permissions (e.g.,chmod 700 ~/.vim/undo) or encrypting the directory via filesystem‑level encryption.Compatibility: the undo file format is binary and not meant for manual editing. Do not try to version‑control undo files; they are specific to the Vim version that created them and may not be readable by other Vim builds.
Actionable closing
Add the two lines to your vimrc, create the undo directory, and test the workflow described above. Once you see :undolist
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.