How do I undo all changes made within the last hour using time-based undo?
Answer
:earlier 1h
Explanation
Vim's undo history is annotated with timestamps, allowing you to travel back to any point in time using :earlier and forward using :later. This is far more powerful than repeated u presses when you need to revert a large batch of changes from a specific time window.
How it works
:earlier {N}h— restore the buffer to its state N hours ago:earlier {N}m— go back N minutes:earlier {N}s— go back N seconds:later {N}m— jump forward in time by N minutes
You can also use counts based on changes ({N}) or file writes ({N}f):
:earlier 5f— go back to how the file looked 5 saves ago:later 1f— jump forward to the next save point
Example
If you have been editing a file for two hours and want to see what it looked like 45 minutes in:
:earlier 1h15m
To come back to the present:
:later 1h15m
Or simply jump to the most recent state:
:later 999h
Tips
:undolistshows the undo tree branches with timestamps, giving you context before you travel- Time-based undo is separate from the regular undo tree — you can still use
uand<C-r>after time-traveling - Undo history is lost when the buffer is unloaded unless you have
undofileenabled (:set undofile). With persistent undo,:earlierworks across sessions