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

How do I navigate Vim's undo tree in chronological order to recover changes from an abandoned undo branch?

Answer

g+

Explanation

Vim's undo history is a tree, not a linear stack. When you undo changes and then make new edits, you create a new branch — and u / <C-r> can only travel along the current branch, leaving previous work unreachable. The g+ and g- commands navigate the undo tree by time, regardless of branching, letting you visit every state the buffer has ever been in.

How it works

  • g+ moves forward in time to the next recorded undo state (newer)
  • g- moves backward in time to the previous recorded undo state (older)

These are distinct from u (undo along current branch) and <C-r> (redo along current branch). While u / <C-r> are branch-aware, g+ / g- cross branch boundaries by jumping to whatever state Vim recorded next or previously by timestamp.

Example

Suppose you type this sequence:

1. Type "hello world"     → state A
2. Press u (undo)         → state B ("hello")
3. Type " vim"            → state C ("hello vim")  ← new branch!

Now <C-r> can only redo toward state C — state A ("hello world") is on an abandoned branch. But g- from state C jumps back to state B, then to state A, recovering "hello world".

You can see the available undo states with :undolist to understand the tree structure before navigating.

Tips

  • Use :earlier {N}s / :later {N}s (seconds) or :earlier {N}m (minutes) for time-based jumps
  • :set undofile enables persistent undo across Vim sessions, making g+ / g- even more powerful
  • g- / g+ respect the count prefix: 5g+ jumps forward 5 undo states at once

Next

How do I inspect a Vim register's full details including its type (charwise, linewise, or blockwise) in Vimscript?