How do I revert a file to its state from a specific time ago?
Answer
:earlier {time} / :later {time}
Explanation
Vim's :earlier and :later commands let you navigate the undo history by wall-clock time rather than by individual undo steps. You can jump to the exact state of your file from 5 minutes ago, 2 hours ago, or even yesterday — as long as the undo history (or persistent undo) covers that period.
How it works
:earlier {time}reverts the buffer to its state from{time}ago:later {time}moves forward in the undo timeline by{time}- Time accepts units:
s(seconds),m(minutes),h(hours),d(days) - You can also use step counts:
:earlier 10goes back 10 undo steps,:later 5goes forward 5 steps :earlier 1freverts to the state at the last file write —fmeans "file writes ago"
Example
You have been editing a file for the past hour and realize the version from 30 minutes ago was better:
:earlier 30m
Vim walks the undo tree backward to the state closest to 30 minutes ago. The buffer now looks exactly as it did at that point.
Went too far back? Move forward 10 minutes:
:later 10m
Or jump to the state from 3 saves ago:
:earlier 3f
This is incredibly powerful when combined with set undofile — your undo history persists across sessions, so :earlier 2d can revert to the file's state from two days ago.
Tips
- Unlike
uand<C-r>which follow undo branches,:earlierand:latertraverse the undo tree chronologically — they visit every state in time order, crossing branch boundaries automatically - Use
:earlier 1fas a quick "revert to last saved" — it returns the buffer to its state at the most recent:w - Combine with
set undofileandset undodir=~/.vim/undodirfor time travel across sessions — without persistent undo, the history is lost when you close the file - After time-traveling, use
:wto save the restored state if you want to keep it - The
g-andg+commands offer similar chronological traversal but step one change at a time, while:earlier/:laterjump by time intervals - Use
:undolistto inspect the undo tree structure and see timestamps for each branch tip - There is no confirmation prompt —
:earlierimmediately changes the buffer, but you can always:laterback oru/<C-r>to adjust