How do I access undo branches that normal u and Ctrl-R skip over?
Answer
g- / g+
Explanation
Vim's undo history is a tree, not a linear stack. When you undo several changes and then make a new edit, the old forward branch still exists — but <C-r> (redo) follows only the latest branch. g- and g+ walk through every state in the undo tree chronologically, including states on branches that u/<C-r> cannot reach.
The problem
- Type
aaa→ undo withu→ typebbb - Now
<C-r>redoesbbb— butaaais on a different branch, unreachable byu/<C-r> g-steps backward through time across all branches, so it can reachaaa
Commands
| Command | Action |
|---|---|
g- |
Go to the previous undo state (by timestamp) |
g+ |
Go to the next undo state (by timestamp) |
:earlier {N}s |
Go to the state as it was N seconds ago |
:later {N}s |
Go forward N seconds from the current state |
:earlier {N}f |
Go back N file-writes ago |
:undolist |
Show all undo branches and their sizes |
Example
Visualize the undo tree:
:undolist
Output:
number changes when saved
3 3 2024/01/15 14:22:05
5 5 2024/01/15 14:22:18
Step backward through every state chronologically:
g- g- g- ... g+ g+
Tips
g-andg+are the simplest way to explore undo branches without understanding the tree structure- For a visual representation, install vim-mundo (
:GundoToggle) or undotree (:UndotreeToggle) - Combine with
:set undofileto persist the entire undo tree across Vim sessions :earlier 10m(go back 10 minutes) is a lifesaver when you realize a mistake made long ago