← back to latest

Undo everything since the last save in one command

COMMAND:earlier 1fUndo everything since the last save in one command

anatomy

:
Enter command-line mode.
earlier
Move backward through undo history by a count of something other than single changes: seconds, minutes, hours, days, or file writes.
1
How many units to go back. One is the usual choice.
f
The unit is file writes. Vim marks the undo state each time you save, and this jumps to the most recent mark.

In the buffer

You saved the file after changing DEBUG to False. Since then you changed the retry count and added a line. Before:

# Configuration
DEBUG = False
MAX_RETRIES = 10
TIMEOUT = 30
           ^

After typing :earlier 1f and <CR>:

# Configuration
DEBUG = False
MAX_RETRIES = 3
              ^

The two edits made after the save are gone. The edit you saved stays. The buffer matches the file on disk again, so the modified flag clears, and the cursor lands where the oldest undone change happened.

When you would reach for it

You saved, then started an experiment: a few substitutions, a deleted block, a rename that touched a dozen lines. It went nowhere and you want the file back the way it was when you last wrote it. Pressing u until it looks right means counting changes you did not count while making them. :earlier 1f uses the save as the landmark and goes straight there, in one step, without touching the disk.

Gotchas

  • If the buffer has no changes since the last save, :earlier 1f does not stay put. It goes back to the write before that one. Run it right after :w and you lose the work between your last two saves. :later 1f brings it back.
  • If you have not saved since opening the file, there is no write to return to, and :earlier 1f goes all the way back to before your first change.
  • Undo history normally lives only as long as the buffer. Close the file and reopen it, and there is nothing to walk back through. Set 'undofile' to keep history on disk between sessions, which also makes f counts work across them.
  • This is not :e!. It moves through undo history, so nothing is lost: <C-r> replays the undone changes one at a time, and :later 1f restores them all at once. Pressing u afterward does the opposite of what you might hope; it keeps going backward from the saved state.

Variants

:later 1f

Moves forward to the next save, or to the newest state if there is no later save. The exact inverse of :earlier 1f.

:earlier 10m

Goes back to the buffer as it was ten minutes ago. The units are s, m, h, and d, for when you remember when things were fine but not which change broke them.

:earlier 3f

Goes back three saves. A larger count than the number of writes stops at the original state of the buffer rather than failing.

lineage

Bram Moolenaar added :earlier and :later in Vim 7.0 in 2006, alongside the undo tree that replaced vi's single level of undo. The f unit came later, in Vim 7.3 in 2010, the same release that introduced persistent undo files. Once undo history could outlive a session, counting by saves became a natural way to navigate it.

-- INSERT --vim-hacks.com022-022-undo-everything-since-the-last-save.html
utf-81,1All