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.
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.
This editor needs a keyboard. Read on, and play it at your computer.
| Keys | What it does |
|---|---|
| u | Undo the last change. |
| Ctrl-r | Redo: undo the undo. |
| 3u / 3Ctrl-r | Undo or redo three changes. |
| U | Undo 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.