Learn Practice Daily golf Reference

How to undo and redo in Vim

In Normal mode, press u to undo the last change and Ctrl-r (Ctrl and r) to redo it. A count repeats them: 3u undoes three changes.

Try it here

Delete, undo, redo

Delete the tip line with dd, take it back with u, then change your mind and redo the delete with Ctrl-r.

1 of 3Delete the tip line with dd
2const total = price * qty;
1const tax = total * 0.2;
3const tip = total * 0.1;
1return total + tax + tip;
NORMALLoading the editor…1/30.0s3:1

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
uUndo the last change.
Ctrl-rRedo: undo the undo.
3u / 3Ctrl-rUndo or redo three changes.
UUndo every change on the last line you edited, all at once. U is itself a change, so u undoes it.
:earlier 5m ↵Go back to the file as it was five minutes ago (10s, 1h, or a count of changes also work).
:later 5m ↵Forward again.
:e! ↵Throw away every change since the last save and reload the file.

What counts as one change

Undo works in whole changes, not keystrokes. A change is one Normal-mode command (dd, x, cw and the word you typed) or everything you typed between entering Insert mode and pressing Esc. So a paragraph typed in one go comes back out with a single u; if you want finer steps, leave Insert mode now and then, the way you'd save a draft.

Moving the cursor is never a change, and neither is saving: u still works after :w.

Undo after closing the file

By default the undo history ends when Vim closes the file. Add set undofile to your config (~/.vimrc, or vim.opt.undofile = true in Neovim's init.lua) and Vim saves the history in a file next to the one you edited (set undodir to keep them all in one folder), so u still works tomorrow.