How do I travel backward through undo history by time rather than by change count?
Answer
:earlier {time}
Explanation
Vim's :earlier and :later commands let you travel through undo history using real-world time intervals instead of individual change counts. This is invaluable when you need to recover a state from "10 minutes ago" without knowing exactly how many edits were made in between.
How it works
:earlier {N}s— go back N seconds:earlier {N}m— go back N minutes:earlier {N}h— go back N hours:earlier {N}d— go back N days:later {N}m— travel forward by N minutes- You can also use file-write count:
:earlier {N}fgoes back to N writes ago
Vim timestamps every change internally, so this works even after closing and reopening a file if undofile is enabled.
Example
You've been editing for 15 minutes and want to see what the file looked like at the start:
:earlier 15m
To return to the most recent state:
:later 15m
Or go back to what the file looked like after the last write:
:earlier 1f
Tips
- Combine with
:set undofileand:set undodirto make undo history persist across sessions — then:earlier 2dcan recover edits from two days ago - Use
:undolistto inspect the undo tree with timestamps to pick the right time reference :earlierand:laternavigate the undo tree, not just a linear undo stack, so they can reach states thatuand<C-r>cannot