Time travel in Vim


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…

How useful was this tip?

Average rating 3.5 / 5. Vote count: 602

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

105 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts