Keep Vim Undo History After You Quit
Enable Vim persistent undo so you can reopen a file and undo changes from previous editor sessions.
15 Jul 2025, 19:17 UTC

The problem: undo disappears when Vim closes
You edit a file, make several changes, and quit Vim. The next day you realize one change was wrong. You reopen the file and press u, but nothing happens. Vim's ordinary undo stack lives in memory, so it disappears when the editor exits.
Persistent undo writes those undo branches to disk. When you reopen the file, Vim can load the saved history and let you step back through changes from earlier sessions.
Enable persistent undo in your vimrc
The vimrc is Vim's startup configuration file, usually located at ~/.vimrc. Persistent undo is available in Vim 7.0 and later. Add these lines:
set undofile
set undodir=~/.vim/undo
undofile tells Vim to save undo history for each buffer. A buffer is Vim's in-memory copy of a file you are editing. undodir sets the folder where those binary undo files live. Without it, Vim writes undo files next to the edited file, which clutters project directories.
Create the directory with restrictive permissions so other users cannot read the undo files:
mkdir -p ~/.vim/undo
chmod 700 ~/.vim/undo
Run mkdir and chmod in your shell as your own user. The set commands run inside Vim, either typed after : or loaded from ~/.vimrc.
Verify it works
Open Vim and run:
:set undofile?
If enabled, Vim prints undofile. If disabled, it prints noundofile. Also inspect the configured directory:
:set undodir?
This prints your configured path. The directory must exist and be writable by your user.
For an end-to-end check:
- Edit a file and make a change.
- Save and quit Vim.
- Reopen the same file.
- Press
u. The change from the previous session should undo.
This describes expected behavior; verify it on your own system before relying on it.
Regular undo versus persistent undo
| Behavior | Regular undo | Persistent undo |
|---|---|---|
| Survives Vim exit | No | Yes |
| Stored where | Memory | Disk, under undodir |
| History keyed by | Current session | Full file path |
Trade-offs and limitations
- Security: Undo files can contain text from edited buffers. Keep
undodirprivate and avoid persistent undo on shared or untrusted hosts without review. - Path sensitivity: Undo history is tied to the full file path. Renaming or moving a file orphans its history because Vim looks up undo files by path.
- Version control: Never add
undodirto a Git repository. Undo files are binary editor state and create merge noise and bloat. - Disk and network filesystems: Buffer changes can write to disk. On slow network filesystems or short-lived containers, that overhead may be unwanted.
Closing
Persistent undo is a small setting with a practical payoff: you can reopen a file and still undo mistakes from earlier sessions. Turn it on, point undodir at a private directory, verify with :set undofile?, and keep the directory out of version control.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.