Time travel in Vim

Vim's built-in undo history allows traveling back and forth in time

Undoing your changes in Vim is as simple as pressing u. Similarly, you can redo with ctrl-r. But Vim’s built-in undo history is capable of far more sophisticated undo and redo commands. You can undo and redo changes in bulk or even with time based commands, allowing you to travel back and forth in time through your change history. Here’s a short summary of commands:

  • :earlier 3 - Undo the last 3 changes

  • :earlier 5m - Go back to the state of the file 5 minutes ago

  • :later 2 - After undoing something, redo the next 2 changes

  • :later 1h - Travel forward through the change history 1 hour

As expected, you can use s, m, h, d for seconds, minutes, hours, and days, respectively. Let’s watch a brief demonstration of this in the following short screencast:

Wait, what was that last bit? That was a completely optional, yet pretty useful plugin called Undo Tree. It shows you a visual representation of your undo history complete with diffs for each change. It’s like a mini version control history without ever having to commit.

Useful? Or lame? Let us know… Vote below so we can adjust the tips based on our audience reaction. And follow us on Twitter for more tips! But first, one more thing…

If you haven’t signed up for Vim Conf Live, you’ll want to check it out. They’re bringing together Vim fans from around the world into a two-day virtual conference with expert speakers and interactive discussions. Andy and I will be there to answer questions and take your feedback on VimTricks.


Was this useful? Help us improve!

With your feedback, we can improve VimTricks. Click a link to vote: