Undo and Redo
Press Esc first, so you are in normal mode. In insert mode these keys type text or do something else.
| Key | What it does |
|---|---|
u | Undoes the last change. |
Ctrl+r | Redoes the last undone change. |
U | Undoes all changes on the last changed line. |
:undo | Same as u. |
:redo | Same as Ctrl+r. |
After each step Vim prints a short note at the bottom of the screen, with the number of changes and how long ago the state you returned to was made. Undo works on the text in Vim; the file on disk changes only when you save.
What Counts as One Change
One change is one command in normal mode, such as dd to delete a line or x to delete a character, or everything you type during one visit to insert mode. If you press i, type three lines, and press Esc, one u removes all three. For smaller undo steps, leave insert mode more often. Moving the cursor is not a change and is not undone.
Undo or Redo Several Steps
Type a count before the key, in normal mode:
5u
3 Ctrl+r
5u undoes the last five changes. For the second line, type 3 and then press Ctrl+r: it redoes three. If you went too far, the opposite key brings the steps back.
Undo a Whole Line with U
Capital U returns the line you changed last to how it was before you started on it, however many edits you made there. It counts as a change itself: u undoes a U, and a second U reverses the first. Most people use u for everything and keep U for a line they have edited beyond repair.
Go Back in Time with :earlier and :later
Press Esc, then type one of these and press Enter:
:earlier 10m
:later 5m
:earlier 3
:earlier 1f
- Time: the units
s,m,h, anddstep by seconds, minutes, hours, or days.:earlier 10mshows the text as it was ten minutes ago. - Count: a plain number steps that many changes.
- Saves:
fsteps by file writes.:earlier 1freturns to the text as it was at the last save, or to the save before that if you have changed nothing since.
:later with the same units moves forward again. Nothing is written to disk until you save.
Undo Branches
If you undo some changes and then type something new, Vim does not throw the undone changes away; it keeps them on a separate branch of its undo tree. u and Ctrl+r move along the current branch only. g- and g+ step through every state in time order, across branches, and :undolist lists the branches. If a redo seems lost, press g- until it comes back.
Keep Undo History After Closing
By default the history ends when you close the file. To keep it, add two lines to ~/.vimrc and create the folder:
set undofile
set undodir=~/.vim/undo//
mkdir -p ~/.vim/undo
chmod 700 ~/.vim/undo
Without undodir, Vim writes the undo file next to the edited file. In a folder of your own, Vim names each undo file after the full path of the edited file, so two files with the same name in different folders do not collide. Undo files hold old versions of what you edited, including a password you removed from a configuration file, which is why the folder should be private.
vi and Neovim
Neovim (nvim) uses the same keys: u, Ctrl+r, U, :earlier, and :later. It already has a default undo folder, which it creates itself, so set undofile in its configuration is enough. The small vi package on some servers runs Vim in a vi-compatible mode, where a second u redoes the change instead of undoing further. If you see that, type :set nocompatible, or install the full vim package.
Throw Away All Unsaved Changes
Warning: :e!, typed in normal mode, reloads the file from disk and drops every change since the last save. In Vim the reload can usually be undone with u while the file stays open, but once you close it the changes are gone. To quit without saving instead, use :q!, as described in how to save and quit in Vim. To jump to the end of the file you are fixing, see how to go to the end of a file in Vim, and to keep edits alive when your connection drops, use the tmux cheat sheet.