tilvim time travel
To undo and redo in Vim, use u and ⌃⇧R (“CTRL-R” in Vim lingo, note the capital R) respectively. This works as you’d expect, but there are a few shortcomings:
-
It is very easy to undo, but dexterously challenging to redo.
-
If you undo, then accidentally make an edit, you can’t redo.
Fortunately Vim has a very powerful undo feature called “undo trees” which you might understand as similar to git branching. Undoing and then starting a new change is just a separate branch in the undo tree. There are a number of ways of interacting with the undo tree1, but a comparatively simple one is undo time travel.
To go to an earlier state, use g-, to go to a later state, g+. If you mess up with undo and redo, you can usually just try g- repeatedly until you’re back where you want to be. While this is not quite as easy to remember as u for undo, I find “go earlier” and “go later” as pretty great mnemonics and really appreciate the symmetry.
These are shorthand for the :earlier and :later commands, which are quite flexible. As an example, :earlier 5m will go to whatever version you were looking at five minutes ago. Amazing.
-
Another example of interacting with the undo tree is
↩:undolist, which shows all leafs in the tree. To learn more about undo, try:help undo-redo.