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

How do I navigate to an older undo state that is not on my current undo branch?

Answer

g-

Explanation

Vim's undo history is a tree, not a linear stack. When you undo some changes and then make new edits, Vim creates a new branch — and u/<C-r> can only traverse the current branch. The g- command steps to the previous undo state by chronological order, regardless of which branch it lives on. Its counterpart, g+, moves forward in time. Together they let you reach states that would otherwise be unreachable with standard undo/redo.

How it works

  • g- — go to the undo state that was recorded immediately before the current one in wall-clock time
  • g+ — go to the next undo state in chronological order
  • {count}g- — jump back count states at once

This is different from u (which backtracks the current branch) and <C-r> (which moves forward on the current branch). After creating a new branch via undoing-then-editing, g- can reach states on the old abandoned branch.

Example

Original: "hello"
1. Type → "hello world"       (state 1)
2. Type → "hello world!"      (state 2)
3. Undo twice → back to "hello" (on state 1, then original)
4. Type → "hello vim"         (state 3 — new branch, state 2 is now unreachable by u/<C-r>)

Now pressing g- steps back through states 3 → original. Pressing g+ goes forward: original → state 1 → state 2 → state 3, traversing all states by time.

Tips

  • :undolist shows all leaf undo states in the tree, which is useful for understanding what g-/g+ can reach
  • :earlier {count}s jumps to the state the buffer was in count seconds ago — an even higher-level time-travel mechanism
  • For a visual undo tree explorer, the undotree plugin (UndotreeToggle) provides a graphical view of all branches

Next

How do I open the directory containing the current file in netrw from within Vim?