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

How do I go back to a specific point in time using Vim's undo history?

Answer

:earlier 5m

Explanation

Vim's :earlier and :later commands let you travel through your undo history using time-based offsets — not just individual changes. You can jump to the exact state your file was in 5 minutes ago, 1 hour ago, or even a specific number of changes back.

How it works

:earlier 5m     " Go back to state from 5 minutes ago
:earlier 1h     " Go back 1 hour
:earlier 30s    " Go back 30 seconds
:earlier 10     " Go back 10 changes
:earlier 3f     " Go back 3 file writes

:later 5m       " Go forward 5 minutes
:later 10       " Go forward 10 changes

Time units

Unit Meaning
s Seconds
m Minutes
h Hours
f File writes (:w counts)
(number) Individual changes

View undo tree

:undolist       " Show all undo branches with timestamps

Why undo branches matter

Vim's undo history is a tree, not a linear stack. If you undo several changes, make new edits, the old timeline becomes a branch. Regular u follows the current branch, but :earlier can reach any state from any branch.

Tips

  • :earlier is a lifesaver when you realize your file was better 10 minutes ago
  • Works even better with persistent undo enabled:
    set undofile
    set undodir=~/.vim/undodir
    
  • With persistent undo, you can recover file states from previous editing sessions
  • g- and g+ traverse undo branches chronologically (including branches that u skips)
  • Consider the undotree plugin for a visual representation of the undo tree

Next

How do I always access my last yanked text regardless of deletes?