vimtricks.wiki Concise Vim tricks, one at a time.

How do I navigate all undo states including branches that u and Ctrl+r can't reach?

Answer

g+ and g-

Explanation

Vim's undo history is a tree, not a linear stack. Standard u and <C-r> only traverse the main branch of that tree — if you undo several changes and then make a new one, the old changes become unreachable via u/<C-r>. The g- and g+ commands traverse every recorded undo state in chronological order, letting you visit branches that would otherwise be permanently lost.

How it works

  • g- — move to the older undo state (earlier in time), including states on other branches
  • g+ — move to the newer undo state (later in time), including states on other branches
  • Both commands walk the undo tree chronologically by timestamp, not by parent-child relationship

This is fundamentally different from u (go to parent node) and <C-r> (go to child node on the current branch). g-/g+ follow a time-ordered traversal of all nodes.

Example

Start with:   "hello"
Type "world" → "helloworld"
Undo with u   → "hello"            (current branch: at root)
Type "vim"   → "hellovim"          (new branch created; "helloworld" is now unreachable via u/Ctrl+r)

At this point, u only reaches "hello""helloworld" is gone from the normal undo path.

But with g- you can travel back to "hello", then g- again to reach "helloworld" — fully restored.

Tips

  • Use :undolist to see all undo leaves with their sequence numbers and timestamps
  • Combine with :earlier / :later for time-based navigation (e.g., :earlier 30s)
  • The undotree plugin provides a visual interface for navigating the full undo tree

Next

How do I insert the entire current line into the command line without typing it?