Struggling to undo your changes in Vim? Wondering how to undo the undo and get your changes back again without closing the file in despair? Looking for a way to jump back in time to an earlier version or branch of your file? Find out here how to undo and redo in Vim ⤓ without losing your nerves or any edits.
On This Page
How to Undo in Vim
Time needed: 1 minute
To undo an action in Vim or Neovim:
- Switch to normal mode.
Here’s how: Typically, press Esc to exit insert mode into normal mode in Vim.
- Press U (lowercase letter u) to undo the last change.
What this does (or undoes): Pressing U undoes one action, for example text input (entering insert mode, typing and exiting insert mode), one pasting text in Vim or one deletion.
Uppercase U: Note that Shift U (uppercase letter u) undoes all changes on the current line.
Command mode: You can also use:undoor the shorter:uin command mode.
More than one action: Prepend the undo-command with a number to undo more than one change at a time; for example, 3 U or:3undowill undo 3 actions.
Graphic Vim: In Vim with a graphical interface and toolbar (GVim etc.), you can also click the Undo button, of course.
Looking for something to do you can undo? How to Change Case in Vim (to Upper, Lower, etc.)
Jump Back in Time with Vim Undo Branches
When you undo actions, and then make changes from that starting point, what you had originally done and then undone is not lost. Like a versioning system, Vim keeps track of changes in branches. You can restore to any of these branches and undo later changes (as well as having undone the original action).
See Vim Restore Points
To view points to which you can restore the state of a file in Vim:
- Press : In normal mode for Vim command mode.
- Type
undolist.
Type less: You can also abbreviate the command toundol. - Press Enter.
Vim will show a table that lets you identify changes and points to which you can restore the file.
| number | identifies the restore point |
| changes | counts the changes to the text |
| when | a date and time, relative if recent |
| saved | the restore point was written to the disk |
Jump to a Restore Point
To jump back to a specific state with Vim undo branches:
- Enter command mode with :.
- Type
ufollowed by the number of the restore point.
Restore points: See above for identifying restore points in Vim
Example: The command can look like:u 12to jump to right after change number 12.
Normal mode jumping: You can also press G - in normal mode to jump back one change from the undo list. - Press Enter.
To go back in time in Vim:
- Type : for command mode.
- Enter
earlierfollowed by relative time.
What is time: You can give time ass(seconds),m(minutes),h(hours) andd(days) ago.
Example::earlier 5hwill jump back 5 hours. - Press Enter.
Redo in Vim
o undo undoing and re-do in Vim:
- Press Ctrl R.
Next restore point: You can also press G + to jump to the next restore point chronologically.
You can also use restore points to jump (back) forward in time:
- Press : for command mode.
- Type
laterfollowed time relative to the current state in the undo list.
Undo list: See above for identifying restore points in Vim
Example::later 3mwill jump ahead 3 minutes - Press Enter.
How to Undo in Vim: FAQ
Can I undo in Vim insert mode while typing?
Yes, but undoing might not accomplish exactly what you’d expect it to do.
The command will still undo one action, that is most typically undoing all input since you entered insert mode.
To undo from insert mode in Vim:
- Press Ctrl O in insert mode.
- Now press U (lowercase letter u).
Can Vim undo changes after I have saved (or reopened) a file?
Yes.
Vim can keep a record of actions for undoing in a file separate from the edited file across sessions.
To enable this kind of persistent undo information in Vim:
- Add
set undofileto your .vimrc file.
By default, Vim keeps these files in the same directory as the original file under the same file name with .un~ appended.
You can collect all undo files in a hidden directory instead. To do so:
- Add
set undodir=~/.vim/undodir(or any other directory, of course) to. your .vimrc file.
To learn about all the details and intricacies of persistent undo, you can examine the built-in help using :help undofile.
(Tested with Vim 9.0–9.1 and Neovim 0.9.5–0.11.5; first published October 2023, last updated December 2025)