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

How do I undo all changes made since the last time I saved the file in a single step?

Answer

:earlier 1f

Explanation

The :earlier {count}f command navigates Vim's undo tree to the state of the buffer at the time of the last (or Nth) file write. :earlier 1f is effectively a "revert to last save" — it instantly discards all unsaved edits regardless of how many individual undo steps that represents, without destroying undo history.

How it works

Vim's undo tree is time-stamped and also tracks file-write events as checkpoints. The f (file) unit in :earlier and :later counts writes:

  • :earlier 1f — revert to the state just after the last :w
  • :earlier 2f — revert to the state after two saves ago
  • :later 1f — move forward to the state after the next write checkpoint

Unlike pressing u repeatedly, :earlier 1f is a single operation regardless of how many changes were made since the last save.

Example

You have made 30 edits across the file since the last :w and want to start over:

:earlier 1f

The buffer is restored to the saved state. Your undo history is preserved — you can use :later 1f to redo everything if you change your mind.

Tips

  • Other time units: :earlier 30s (30 seconds ago), :earlier 5m (5 minutes ago), :earlier 2h (2 hours ago)
  • :later 1f is the inverse: advance to the state right after the next save event
  • :undolist shows the undo tree branches with timestamps
  • This does NOT reload the file from disk — it uses the in-memory undo tree; for a true reload use :e!
  • Combine with :set undofile to persist undo history across sessions

Next

How do I trigger a custom user-defined completion function in insert mode?