How to Undo and Redo in Vim / Vi

Vim is a terminal-based text editor that comes preinstalled on macOS and almost all Linux distributions. It keeps a detailed undo history during a session, with the number of remembered changes controlled by the undolevels option, so you can undo mistakes and redo reverted changes.
This guide explains how to undo and redo changes in Vim, undo all changes on a line, navigate the undo history by time, and work with undo branches.
Undo
The u command undoes the most recent change. Vim reverts changes in the reverse order they were made.
- Press
Escto make sure you are in normal mode. - Press
uto undo the last change.
Each press of u undoes one more change. To undo multiple changes at once, type a count before the command. For example, 4u undoes the last four changes.
You can also use the ex command form :undo or :u, which works the same way.
The undo command reverts changes made by any operation, including delete , paste , find and replace , and insert mode edits.
How Vim Groups Changes
In normal mode, each command (such as dd, p, or x) counts as one change. In insert mode, everything you type from the moment you enter insert mode until you press Esc counts as a single change. If you enter insert mode, type five lines, and press Esc, pressing u removes all five lines at once.
Undo All Changes on a Line
The U (uppercase) command undoes all recent changes on a single line: the last line you changed. Unlike u, which steps through changes one at a time, U reverts the entire line to its original state in one step.
The cursor does not have to be sitting on that line. If you edit line 5, move down to line 20 to read something, then press U, Vim still reverts line 5. The target only moves once you change a different line.
Note that U is itself a change that can be undone with u.
Redo
The Ctrl+r command redoes a change that was undone with u. It reverses the undo, restoring the text to the state before you pressed u.
- Press
Escto make sure you are in normal mode. - Press
Ctrl+rto redo the last undone change.
Like u, the redo command accepts a count. For example, press 4, then Ctrl+r to redo the last four undone changes. You can also use the ex command form :redo.
Each undo can be reversed by a redo, and each redo can be reversed by an undo.
Undo in Vi
On most Linux systems and on macOS, typing vi starts a Vim build, so everything above applies unchanged. Debian and Ubuntu route vi through the alternatives system to a Vim build such as vim.tiny, Fedora and RHEL ship /usr/bin/vi from the vim-minimal package, and on macOS /usr/bin/vi is a symbolic link to vim. To check what you are running, ask it for its version:
vi --version | head -1VIM - Vi IMproved 9.1 (2024 Jan 02, compiled Apr 18 2026 20:29:32)Original vi keeps only one level of undo, and there u works as a toggle: the second press undoes the undo instead of stepping back another change. There is no separate redo command, since u already does that job. Ctrl+r is a Vim addition.
Current Vim releases normally use nocompatible mode even when no user vimrc exists. Since Vim 8.0, a normal startup without a user vimrc loads defaults.vim, which disables compatible mode. Vi-compatible behavior can still appear when Vim is started with -C or -u NONE, when an older or stripped-down installation lacks defaults.vim, or when configuration enables compatible explicitly.
The u flag in cpoptions controls how the undo commands behave. With the flag present, u toggles the previous undo and Ctrl+r repeats the previous undo. Removing the flag restores the normal Vim behavior, where u moves backward through changes and Ctrl+r moves forward:
:set cpoptions-=uThe undolevels option controls the history depth separately. Setting set undolevels=0 forces the single-level vi behavior even in a normal Vim session, and set undolevels=-1 turns undo off completely, which is mostly worth knowing because it explains a Vim that suddenly refuses to undo anything.
Time-Based Undo
Vim can undo and redo changes based on time rather than individual operations. The :earlier command moves backward in time, and the :later command moves forward.
To revert the file to its state ten minutes ago:
:earlier 10mTo move forward five seconds from the current undo state:
:later 5sThe supported time units are:
s- Secondsm- Minutesh- Hoursd- Daysf- File writes
The f unit counts writes instead of clock time. If you have edited the file since the last :w, then :earlier 1f puts it back to the state it had when you last saved. That is the quickest way to throw out everything typed since the save, and unlike :q!
it does not cost you the session.
You can also pass a number without a unit to move by a count of states. For example, :earlier 3 steps back three states and :later 3 steps forward three. These counts walk the undo tree the same way g- and g+ do, described in the next section, so they are not always the same as pressing 3u.
This is useful when you have made many changes and want to return to a known good state without pressing u repeatedly.
Undo Branches
In most editors, undo history is linear, so once you undo a change and make a new edit, the undone change is lost. Vim works differently. It stores every change in an undo tree, so no edit is ever permanently lost.
Here is an example to illustrate the concept:
- Open a file and type “line one” on the first line.
- Press
uto undo. - Type “line two” on the first line.
At this point, a linear undo system would have lost “line one”. Vim keeps both versions as separate branches in the undo tree.
To navigate between undo branches:
g-- Move to the previous state in the undo tree (goes to older branches)g+- Move to the next state in the undo tree (goes to newer branches)
The g- and g+ commands walk through every state the file has been in, including states on different branches. The standard u and Ctrl+r commands only move within the current branch.
Persistent Undo
By default, Vim discards the undo history when you close a file. With persistent undo enabled, Vim saves the undo history to a file on disk, allowing you to undo changes even after closing and reopening the file.
To enable persistent undo, add the following lines to your ~/.vimrc:
set undofile
set undodir=~/.vim/undodirCreate the undo directory if it does not exist:
mkdir -p ~/.vim/undodirWith this configuration, Vim saves the undo history for each file in ~/.vim/undodir. You can undo changes from previous editing sessions as if you never closed the file.
Quick Reference
For a printable quick reference, see the Vim cheatsheet .
| Command | Description |
|---|---|
u | Undo the last change |
4u | Undo the last four changes |
U | Undo the latest changes on the last changed line |
Ctrl+r | Redo the last undone change |
4 then Ctrl+r | Redo the last four undone changes |
:earlier 10m | Revert to the state 10 minutes ago |
:earlier 1f | Revert to the state at the last file write |
:later 5s | Move forward 5 seconds in undo history |
g- | Move to the previous state in the undo tree |
g+ | Move to the next state in the undo tree |
:undolist | Show the list of undo branches |
:undo 12 | Jump to change number 12 shown by :undolist |
FAQ
What is the difference between u and U?
Lowercase u undoes the last change, stepping back one operation at a time. Uppercase U undoes the latest changes on the last line you changed, regardless of the current cursor position.
Can I undo changes after saving a file?
Yes. Vim keeps the undo history in memory for the entire session, even after saving with :w
. To undo changes after closing and reopening a file, enable persistent undo with set undofile in your ~/.vimrc.
How do I undo everything since the last save?
Run :earlier 1f to send the file back to the state it had at the last write. If you want to discard every unsaved buffer change and reload the current contents from disk, run :e! instead.
Is there a limit to how many changes I can undo?
The undolevels option sets the maximum number of changes Vim remembers. The default on Unix systems is 1000. Increasing it retains more history but also uses more memory.
What happens if I undo, then make a new edit?
Vim creates a new branch in the undo tree. The undone changes are not lost; you can reach them with g- and g+, which walk through all states in the tree.
How do I see the undo history?
Use :undolist to see a summary of undo branches, including the number of changes and the time of each branch. Every line starts with a change number, and :undo followed by that number jumps straight to the matching state, so :undo 108 takes you to the text as it was right after change 108.
Conclusion
Enable persistent undo with set undofile if you want history to survive across sessions. Without it, u and Ctrl+r only work until you close the file. Undo is also the safety net for whole-file edits, such as selecting all text and replacing it
.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page