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

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:

  1. Type helloworld → undo to hello → type earth

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 :undolist to inspect the undo tree and see how many branches exist and their timestamps.
  • Combine with :earlier / :later for time-based traversal of the same undo tree.
  • Plugins like undotree or mundo provide a visual graph of the undo tree, making g- / g+ navigation much easier to reason about.

Next

What is the difference between zt and z-Enter when scrolling the current line to the top of the screen?