Step 4 of 5

7. Undo And Redo

Time traveling with Vim

g- and g+ are useful, but there’s actually a much easier way to recover lost changes like the ones we talked about in the previous section.

Vim has two useful commands for time traveling in the document history: :earlier and :later. If you want to revert to the way that your document was 10 minutes ago, you can use the command :earlier 10m. To get back, use :later 10m.

There are a few ways you can use this:

  • :earlier 1 is the same as g-, and :later 1 is the same as g+.
  • You can use time units like 10s, 10m, 10h, and 10d for seconds, minutes, hours, and days respectively.
  • You can use :earlier 3f, for example, to revert to the way that the file looked 3 file writes ago.

Work through the checklist below to time travel by a single change. Once you have the hang of it, make a few more edits, wait a short while, and try traveling with a time unit — like :earlier 10s.

  1. Enter insert mode The mode indicator will flip to INSERT — press i.
  2. Make a change, then return to normal mode Type anything you like, then hit Escape.
  3. Travel back in time Your change is rolled back — type :earlier 1 and hit Enter.
  4. Travel forward again To bring the change back, type :later 1 and hit Enter.
Enter insert mode The mode indicator will flip to INSERT — press i.

Next: Make a change, then return to normal mode

Loading editor…