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

How do I undo all changes made in the last N minutes using time-based undo?

Answer

:earlier {N}m

Explanation

Vim's :earlier command lets you travel back through the undo history by wall-clock time rather than by the number of changes. This is invaluable when you've made many small edits and want to get back to how the file looked "five minutes ago" without counting individual undo steps.

How it works

  • :earlier {N}m — revert the buffer to its state N minutes ago
  • :earlier {N}s — revert N seconds ago
  • :earlier {N}h — revert N hours ago
  • :earlier {N}f — revert to the state at the Nth earlier file write

The companion command :later {N}m moves forward in time — useful if you went too far back.

Vim tracks a timestamp for every undo state, so the time-based lookup is exact even across sessions (when undofile is enabled).

Example

You've been editing for 10 minutes and realize the last 3 minutes of changes were a mistake:

:earlier 3m

The buffer is instantly reverted to where it was 3 minutes ago. If you want to see what you undid:

:later 1m

Tips

  • Pair with :set undofile and undodir to persist undo history across Vim sessions — :earlier works across restarts
  • :undolist shows the tree of undo states with their timestamps, helping you pick a target time
  • Unlike u (undo), :earlier navigates the entire undo tree, not just the linear branch

Next

How do I use a count with text objects to operate on multiple text objects at once?