I'm trying to understand how to move in the undo tree with the following default mappings:
Here's my understanding of things, which is probably wrong or incomplete. Each time we make a new edit, a new leaf is created in the undo tree. The more we make edits, the more leafs are added along a branch.
To move along the branch, we can use
u to go back to the original version of the buffer, and
<C-R> to go forward to the newest version.
This can be confirmed with the simple following test:
- create an empty buffer,
- insert the number
<C-A>to increment it to
2, again to
3and again to
<C-R>, we can go back to each state of the buffer: empty,
If at one point, we go backward and make a new edit from a past leaf, we create a new branch. The leafs which are not between the root of the tree (the original buffer) and the beginning of the new branch (the last edit we just made) can't be accessed with
Going back to the example, this can be confirmed:
- from the buffer's version
uto go back to
uto undo the last edit
u doesn't bring us back to
4 but to
We can't go back anymore to
<C-R> because they only move along the shortest path between the root of the tree and the most recent version of the buffer which we've visited.
4 is not on the shortest path between the original empty buffer and
5 was created from
To go back to any leaf of the undo tree, we can use
<C-R>, they move along a time axis on which every leaf is placed in the order it was created.
To confirm this:
- undo with
uup to the original empty buffer
g+5 times to reach
g-5 times to get back to
1and the empty buffer
However, there's one case I don't understand.
5, we hit
u to get back to
g+, we get
But on the time axis, the nearest leaf from
3 is not
So why does Vim bring us to
5 instead of