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.