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

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}f goes 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 undofile and :set undodir to make undo history persist across sessions — then :earlier 2d can recover edits from two days ago
  • Use :undolist to inspect the undo tree with timestamps to pick the right time reference
  • :earlier and :later navigate the undo tree, not just a linear undo stack, so they can reach states that u and <C-r> cannot

Next

How do I insert the current date or time into the buffer using Vim's built-in expression evaluation?