How do I navigate Vim's undo tree branches to access states that u and Ctrl-R can't reach?
Answer
g- and g+
Explanation
Vim's undo history is a tree, not a linear stack. Every time you undo and then make a new change, the previous "redo" branch is still preserved — it just becomes unreachable with u and <C-r>. The g- and g+ commands let you walk the full undo tree chronologically, visiting every state in order of when it was created.
How it works
g-— move to the older undo state in time (equivalent to going back one step in recording order, regardless of branch)g+— move to the newer undo state in time{count}g-/{count}g+— jump multiple states at once
Unlike u (which undoes the last change on the current branch) and <C-r> (which redoes on the current branch), g- and g+ traverse across branches, giving you access to every editing state Vim has ever recorded.
Example
Suppose you:
- Type
hello→world→ undo tohello→ typeearth
Now <C-r> takes you to earth. But world (the abandoned branch) is gone from <C-r> — yet still accessible:
g- → restores to "world" (the abandoned redo branch)
g+ → moves forward again to "earth"
Tips
- Use
:undolistto inspect the undo tree and see how many branches exist and their timestamps. - Combine with
:earlier/:laterfor time-based traversal of the same undo tree. - Plugins like
undotreeormundoprovide a visual graph of the undo tree, makingg-/g+navigation much easier to reason about.