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

How do I undo or redo to a specific point in time using time-based undo navigation?

Answer

:earlier {time} and :later {time}

Explanation

Vim's :earlier and :later commands let you navigate the undo history by elapsed time rather than by edit count. Instead of pressing u dozens of times, you can jump directly to the state of your file as it was N minutes or hours ago — a lifesaver when you need to recover from a long detour.

How it works

Vim timestamps every edit internally. :earlier and :later accept a time argument and jump to the nearest undo state at that point:

  • :earlier 10m — revert to the file state from 10 minutes ago
  • :earlier 2h — revert to how the file looked 2 hours ago
  • :later 10m — redo forward 10 minutes (the inverse of :earlier)

Time units accepted:

  • s — seconds
  • m — minutes
  • h — hours
  • d — days
  • f — file writes (:earlier 1f goes back to the state at the last :w)

These commands move through the same undo tree that u / <C-r> use, but they select the branch by timestamp rather than by step count.

Example

" Spent 30 minutes going in the wrong direction — revert:
:earlier 30m

" Oops, went too far back — move forward 10 minutes:
:later 10m

" Recover the file as it was at the last :w save:
:earlier 1f

Tips

  • Use :undolist to inspect the undo tree and see timestamps of branches.
  • :earlier {N}f is especially handy as a "soft revert" — it goes back to the last saved state while leaving the undo history intact so you can :later forward again.
  • Enable :set undofile to persist undo history across sessions; time-based navigation then works even after reopening a file.

Next

How do I open the directory containing the current file in netrw from within Vim?