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

How do I revert a file to its state from a specific time ago?

Answer

:earlier {time} / :later {time}

Explanation

Vim's :earlier and :later commands let you navigate the undo history by wall-clock time rather than by individual undo steps. You can jump to the exact state of your file from 5 minutes ago, 2 hours ago, or even yesterday — as long as the undo history (or persistent undo) covers that period.

How it works

  • :earlier {time} reverts the buffer to its state from {time} ago
  • :later {time} moves forward in the undo timeline by {time}
  • Time accepts units: s (seconds), m (minutes), h (hours), d (days)
  • You can also use step counts: :earlier 10 goes back 10 undo steps, :later 5 goes forward 5 steps
  • :earlier 1f reverts to the state at the last file write — f means "file writes ago"

Example

You have been editing a file for the past hour and realize the version from 30 minutes ago was better:

:earlier 30m

Vim walks the undo tree backward to the state closest to 30 minutes ago. The buffer now looks exactly as it did at that point.

Went too far back? Move forward 10 minutes:

:later 10m

Or jump to the state from 3 saves ago:

:earlier 3f

This is incredibly powerful when combined with set undofile — your undo history persists across sessions, so :earlier 2d can revert to the file's state from two days ago.

Tips

  • Unlike u and <C-r> which follow undo branches, :earlier and :later traverse the undo tree chronologically — they visit every state in time order, crossing branch boundaries automatically
  • Use :earlier 1f as a quick "revert to last saved" — it returns the buffer to its state at the most recent :w
  • Combine with set undofile and set undodir=~/.vim/undodir for time travel across sessions — without persistent undo, the history is lost when you close the file
  • After time-traveling, use :w to save the restored state if you want to keep it
  • The g- and g+ commands offer similar chronological traversal but step one change at a time, while :earlier/:later jump by time intervals
  • Use :undolist to inspect the undo tree structure and see timestamps for each branch tip
  • There is no confirmation prompt — :earlier immediately changes the buffer, but you can always :later back or u/<C-r> to adjust

Next

How do I edit multiple lines at once using multiple cursors in Vim?