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

How do I access undo branches that normal u and Ctrl-R skip over?

Answer

g- / g+

Explanation

Vim's undo history is a tree, not a linear stack. When you undo several changes and then make a new edit, the old forward branch still exists — but <C-r> (redo) follows only the latest branch. g- and g+ walk through every state in the undo tree chronologically, including states on branches that u/<C-r> cannot reach.

The problem

  1. Type aaa → undo with u → type bbb
  2. Now <C-r> redoes bbb — but aaa is on a different branch, unreachable by u/<C-r>
  3. g- steps backward through time across all branches, so it can reach aaa

Commands

Command Action
g- Go to the previous undo state (by timestamp)
g+ Go to the next undo state (by timestamp)
:earlier {N}s Go to the state as it was N seconds ago
:later {N}s Go forward N seconds from the current state
:earlier {N}f Go back N file-writes ago
:undolist Show all undo branches and their sizes

Example

Visualize the undo tree:

:undolist

Output:

number  changes  when               saved
     3       3  2024/01/15 14:22:05
     5       5  2024/01/15 14:22:18

Step backward through every state chronologically:

g-  g-  g-  ...  g+  g+

Tips

  • g- and g+ are the simplest way to explore undo branches without understanding the tree structure
  • For a visual representation, install vim-mundo (:GundoToggle) or undotree (:UndotreeToggle)
  • Combine with :set undofile to persist the entire undo tree across Vim sessions
  • :earlier 10m (go back 10 minutes) is a lifesaver when you realize a mistake made long ago

Next

How do I run a search and replace only within a visually selected region?