The Undo Tree

The trap

Type a line. Press u to undo it. Now type something different.

That second edit did something invisible: Vim did not throw the first line away. It started a second branch. Your undo history is a tree, and you just grew a new limb.

Press u and Ctrl-r all day and you will only ever see the two states on your current path. The first line is off that path — in most editors, gone forever.

:undolist

:undolist
number changes  when               saved
     1       1  70 seconds ago
     2       1  43 seconds ago

Two branches, each with its own change number and timestamp. Both still kept.

g- and g+

  • g- — go back one state in time, regardless of which branch it is on
  • g+ — go forward in time

These do not walk the branch. They walk the chronology. That is how you reach text that u can never get back to.

Vim confirms it in the message line: 3 changes; after #1 91 seconds ago.