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

How do I undo or redo all changes made within a specific time window in Vim?

Answer

:earlier {N}m and :later {N}m

Explanation

Vim's :earlier and :later commands let you travel through your edit history by wall-clock time rather than by individual undo steps. This is invaluable when you want to restore your buffer to the state it was in five minutes ago without manually pressing u hundreds of times.

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}f — go back N file writes
  • :later {N}m — move forward N minutes in time

These commands traverse Vim's undo tree to the nearest state that matches the requested timestamp. They respect the full undo tree, not just the linear chain that u / <C-r> follows.

Example

You've been editing for 10 minutes and made a mess. To restore the file to how it looked 5 minutes ago:

:earlier 5m

To go back to the last saved version:

:earlier 1f

To redo up to 2 minutes forward from the current undo state:

:later 2m

Tips

  • Pair with :undolist to see a summary of the undo tree branches and their timestamps.
  • The undo history is preserved across sessions if 'undofile' is enabled (:set undofile), so :earlier 1h can reach changes from previous Vim sessions.
  • If Vim cannot reach the exact time requested it will stop at the closest available state and report the actual time it landed on.

Next

How do I make Vim show error messages that are silently swallowed inside :silent! or :try blocks?