vimtricks.wiki Concise Vim tricks, one at a time.

How do I undo changes back to how the file looked 5 minutes ago?

Answer

:earlier

Explanation

Vim's undo history is not just a linear list of changes — it records timestamps too. The :earlier and :later commands let you travel through undo history by time rather than by undo count, making it easy to recover from a session where you made many edits in a short burst.

How it works

  • :earlier {N}s — move the undo state back by N seconds
  • :earlier {N}m — move back by N minutes
  • :earlier {N}h — move back by N hours
  • :earlier {N}f — move back by N file saves
  • :later {N}m — move forward by N minutes (redo direction)
  • All accept counts: :earlier 5m goes back 5 minutes; :later 30s goes forward 30 seconds

These commands set the buffer state to the undo tree node closest to that timestamp — they do not simply run u multiple times, so they work even after you have branched the undo tree with new edits after undoing.

Example

You realize you deleted a function 10 minutes ago and have made many changes since:

:earlier 10m

Your buffer reverts to its state from 10 minutes ago. Review the recovered code, yank what you need, then:

:later 10m

You are back to the present with the function text in your clipboard ready to paste.

Tips

  • :undolist shows the undo tree entries with timestamps — use it to orient yourself before using :earlier
  • Pair with :earlier 1f to jump back to how the file looked just before your last :w save
  • The time resolution depends on how frequently Vim records undo snapshots (controlled by 'undolevels')

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?