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 byNseconds:earlier {N}m— move back byNminutes:earlier {N}h— move back byNhours:earlier {N}f— move back byNfile saves:later {N}m— move forward byNminutes (redo direction)- All accept counts:
:earlier 5mgoes back 5 minutes;:later 30sgoes 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
:undolistshows the undo tree entries with timestamps — use it to orient yourself before using:earlier- Pair with
:earlier 1fto jump back to how the file looked just before your last:wsave - The time resolution depends on how frequently Vim records undo snapshots (controlled by
'undolevels')