Vim always surprises me in ways I never expected, and today it taught me that it has a cure for my discontent with the way most applications implement the undo functionality.

Say you type a 1000 word essay into your favourite text editor which is not vim, then press the undo button until the entire essay has disappeared, and then type something else. In most text editors your essay has been lost, but not in Vim.

In the above example Vim would maintain two separate undo branches, one with your essay and one with the content you wrote after undoing your essay. Its really quite neat and you should definitely read the documentation to appreciate the details, but here is a basic rundown:

  • type foo
  • press u once so that the editor is empty again
  • type bar

then use the :undol[ist] command. You should see the following:

number changes when saved 1 1 9 seconds ago 2 1 4 seconds ago

The number is the number of changes since the beginning of the document, while changes are the number of changes since starting that branch of the undo history. Currently you will be on the second branch, and in any other text editor you would be unable to get to the first branch. In Vim you simply use u {n} where n is the change number you would like to jump to.