How do I view all available branches in Vim's undo tree to recover changes that u and Ctrl-R can't reach?
Answer
:undolist
Explanation
Vim's undo history is a tree, not a linear stack. Every time you make a change after undoing previous changes, a new branch is created — meaning earlier edits can become unreachable with plain u and <C-r>. The :undolist command displays all leaf nodes of the undo tree, letting you navigate to otherwise hidden states.
How it works
Running :undolist shows a table with these columns:
- number — the undo sequence number, used with
:undo {n} - changes — how many changes this branch contains
- when — timestamp of the last change on this branch
- saved — marker if this state was written to disk
Once you identify a branch number, use :undo {n} to jump directly to that undo state.
Example
:undolist
number changes when
3 2 35 seconds ago
7 5 just now
To restore undo state 3:
:undo 3
Tips
:undo 0returns to the initial (unmodified) state of the buffer- Use
g-andg+to walk through all undo states in chronological order, including branches thatuskips - Enable
:set undofileto persist the undo tree across sessions so you can recover from previous editing sessions - For a visual undo tree browser, the
undotreeplugin wraps these commands in a navigable sidebar