How do I manually save the undo history for a file to a separate undo file in Vim?
Answer
:wundo {file}
Explanation
The :wundo {file} command writes the current buffer's entire undo history to a file on disk. Its counterpart, :rundo {file}, reads that history back, restoring multi-level undo even after the buffer has been closed and reopened. This is the manual, per-session equivalent of the :set undofile option, which handles the process automatically.
How it works
:wundo myfile.un~— writes the undo tree for the current buffer tomyfile.un~:rundo myfile.un~— restores that undo tree into the current buffer- The undo file is binary and Vim-version-specific; it stores the full tree including branches
- If the buffer has been modified since the undo file was saved, Vim warns that the undo history may not match
This is useful when you want to checkpoint undo history before a risky batch operation, share undo state with a colleague, or manage undo files in a location not covered by undodir.
Example
" Save undo history before a complex refactor
:wundo /tmp/myfile.undo
" ... perform many changes ...
" Restore if the refactor went wrong
:e myfile.txt
:rundo /tmp/myfile.undo
u " now undo individual steps
Tips
- Use
:set undofile undodir=~/.vim/undo//to enable automatic per-file undo persistence instead of managing files manually :wundoonly saves undo history for the current state — run it before, not after, the operation you might want to undo- The undo file format includes a hash of the file contents; if the file changes independently,
:rundomay refuse to load with an error