How to Undo in Vim: Undo, Redo, and Recover Any Change
To undo in Vim, press Esc to make sure you are in Normal mode, then press u. That one keystroke reverses your most recent change, whether you deleted a line, pasted the wrong text, or typed over something important. Press u again to undo the change before that, and use Ctrl+R to redo anything you undid by mistake. Everything in this guide builds on that simple idea, so if you only remember one thing, remember this: Esc, then u.
Vim’s undo system is far more powerful than the single-step undo in most editors. Behind that one key is a branching undo tree that remembers every change you made, lets you jump back to any earlier state, and can even survive closing and reopening a file. Once you understand how it works, you will never lose work to a wrong keystroke again.
How to Undo in Vim
Undoing in Vim only works in Normal mode, which is the mode Vim starts in and the mode you return to by pressing Esc. If you are typing text, you are in Insert mode, and the letter u would simply be typed into your document. So the first step is always the same: press Esc, then press u.
Each press of u undoes one change. Vim counts a change as a single editing action, which usually means one insertion of text, one deletion, or one paste. If you typed a whole sentence and then pressed Esc, that whole sentence counts as one change, so a single u removes all of it. If you deleted a line with dd, one u brings the whole line back.
To undo in Insert mode without leaving it, press Ctrl+O and then u. The Ctrl+O combination lets you run one Normal mode command and then returns you straight to Insert mode, so you can undo a mistake and keep typing without breaking your flow.
It helps to build the habit of pressing Esc the moment something looks wrong. Vim does not have a visible undo button, and there is no Edit menu to click, so your fingers need to know the way. Press Esc, press u, look at the screen, and repeat until the mistake is gone. If you go too far, redo with Ctrl+R, which is covered below.
How to Redo in Vim
Redoing reverses an undo. If you pressed u one too many times, press Ctrl+R in Normal mode to redo the change you just undid. Like undo, redo can be repeated: each Ctrl+R steps one change forward again.
There is also the U command, capital U, which undoes all of the most recent changes on the current line. If you edited a single line several times and want it back the way it was, move the cursor to that line and press U. If you change your mind, pressing u afterwards undoes the U command itself, so nothing is ever truly lost while the undo history is intact.
The redo shortcut is worth memorizing alongside u because the two keys work as a pair. Undo steps backward through your editing history, and redo steps forward again. Together they let you walk back and forth through your recent changes until the file looks right.
Vim Undo and Redo Commands at a Glance
These are the commands you will use most often. Keep this table nearby while you are learning, and within a week you will not need it.
| Command | What it does | Mode |
|---|---|---|
| u | Undo the last change | Normal |
| Ctrl+R | Redo the last undone change | Normal |
| U | Undo all recent changes on the current line | Normal |
| 3u | Undo the last three changes | Normal |
| :undo 3 | Undo three changes | Command-line |
| :earlier 10m | Go back to how the file looked 10 minutes ago | Command-line |
| :later 5m | Move forward 5 minutes in the undo history | Command-line |
| g- | Move to the previous undo branch state | Normal |
| g+ | Move to the next undo branch state | Normal |
| :undolist | Show all undo branches | Command-line |
How to Undo Multiple Changes at Once
You do not have to press u dozens of times to reverse a big mistake. Vim accepts a count before most commands, so 3u undoes the last three changes in one step, and 10u undoes ten. This is the fastest way to back out of a large block of edits when you know roughly how many changes you want to reverse.
For precise control, use the :undo command with a number. Typing :undo 5 reverses the last five changes, and :undo without a number reverses one change, exactly like pressing u. The command-line form is useful when you want to be explicit, or when you are writing the steps down for someone else.
Another handy trick is undoing back to the last time you saved. Vim records when the file was written, so :earlier 1f takes the buffer back to the state it was in at the previous file write. That is the closest Vim comes to a rewind button for your last save, and it works even if you made dozens of changes since then.
How to Undo to an Earlier Point in Time
Sometimes you do not know how many changes to undo; you know when the file was last good. That is what :earlier and :later are for. They move the buffer to the state it was in at a given time, and they understand seconds, minutes, and hours.
Try :earlier 10m to see the file as it was ten minutes ago, or :earlier 1h to go back a full hour. If you overshoot, :later 5m moves five minutes forward again. You can also use :earlier 30s for a very recent change, or :earlier 1d to jump back a whole day, provided the undo history reaches that far.
These time-based commands only work within the history Vim still holds. By default, Vim keeps up to 1,000 changes per file in memory, controlled by the undolevels setting. That is plenty for a normal editing session, but if you need deeper history, add set undolevels=10000 to your configuration file. Vim keeps its configuration in a hidden file called .vimrc in your home directory, and if you need to find hidden files like that, our guide on how to show hidden files in Linux explains both the terminal and the file manager methods.
The Vim Undo Tree Explained
Most editors keep a straight line of undo history: each new change erases the redo path. Vim is different. It keeps a tree. When you undo a change and then make a different edit instead of redoing, Vim does not throw the undone change away. It saves it as a branch of the tree, and you can return to it later.
This is genuinely useful. Imagine you write a paragraph, undo it, and write a different paragraph. In most editors, the first paragraph is gone forever. In Vim, both versions are still in the tree. Press g- to move to an older state in the tree, and g+ to move to a newer one. Unlike u and Ctrl+R, which only walk the most recent branch, g- and g+ can step across branches, so you can recover text you thought you had replaced.
To see the branches, run :undolist. It lists each saved undo state with a number and a timestamp. You can jump straight to any of them with :undo followed by the number, for example :undo 12. The tree is what makes Vim’s undo system so forgiving, and it is the reason experienced Vim users rarely lose work.
How to Keep Undo History After Closing Vim
By default, Vim forgets everything when you close a file. Quit, reopen, and the undo history is empty. That is fine for quick edits, but painful when you reopen a file the next day and want to undo yesterday’s change. The fix is persistent undo, and it takes two lines in your configuration.
Add these lines to your .vimrc file:
set undofile set undodir=~/.vim/undo
The first line tells Vim to save undo history to a file, and the second tells it where to keep those files. You may need to create the directory first with mkdir -p ~/.vim/undo in your terminal. To check what is already in a directory, the ls -la command lists all files, including hidden ones like .vimrc, with their permissions and sizes. Once persistent undo is on, you can close Vim, reopen the same file tomorrow, and still undo changes from the previous session, including jumping back with :earlier 1d.
One thing to know is that undo files are stored per file and can grow over time, but they are small, usually only a few kilobytes. If you ever want to clear the history for a file, simply delete its undo file from the undo directory. Otherwise, persistent undo is a set-and-forget feature that quietly protects your work.
Why Undo Is Not Working in Vim
If u does nothing, run through this checklist before assuming something is broken. First, make sure you are in Normal mode. Press Esc, then try u again. In Insert mode, u is just a letter, and in Visual mode it does something else entirely, so the mode matters.
Second, check that there is actually something to undo. If you just opened the file and have not changed anything, u has nothing to reverse, and Vim will flash or beep to tell you so. The same happens if you have already undone every change in the session.
Third, check whether the file was reloaded. Commands like :e! reload the file from disk and throw away the undo history along with your unsaved changes, so after a reload there may be nothing to undo. Similarly, if undo worked earlier but stopped after you restarted Vim, the history was lost when the session ended, which means you need the persistent undo setup described above.
Finally, check the undolevels setting by typing :set undolevels? in the command line. If it shows 0, undo is disabled entirely. Set it to a sensible number with :set undolevels=1000, or add set undolevels=1000 to your .vimrc to make the change permanent.
Frequently Asked Questions
How do I undo in Vim?
Press Esc to enter Normal mode, then press u. Each press undoes one change. Pressing u repeatedly walks backward through your edits, and Ctrl+R redoes anything you undid by mistake.
How do I undo in Vim without leaving Insert mode?
Press Ctrl+O and then u. The Ctrl+O combination runs a single Normal mode command and returns you to Insert mode, so you can undo one change and keep typing without interruption.
How do I redo in Vim?
Press Ctrl+R in Normal mode. Each press redoes one change that you previously undid. If you undid too much, keep pressing Ctrl+R until the file is back to the state you want.
Can I undo after saving a file in Vim?
Yes. Saving with :w does not clear the undo history, so you can keep pressing u after a save. The history is only lost when you close the file, unless you have enabled persistent undo with set undofile in your .vimrc.
How do I undo everything in Vim?
There is no single command that empties the whole file, but you can get there quickly. Use :earlier 1f to return to the state at the last save, use a large count like 100u to undo many changes at once, or use U to undo all recent changes on the current line. To discard everything and start over from the saved file, use :e! to reload it from disk.