How To Undo And Redo In Vim / Vi - Linuxize
Maybe your like
Vim is a terminal-based text editor that comes preinstalled on macOS and almost all Linux distributions. It keeps a full history of every change you make during a session, so you can undo mistakes and redo reverted changes at any time.
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 Esc to make sure you are in normal mode.
- Press u to 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 changes made to the current line since the cursor moved to it. Unlike u, which steps through changes one at a time, U reverts the entire line to its original state in one step.
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 Esc to make sure you are in normal mode.
- Press Ctrl+r to redo the last undone change.
Like u, the redo command accepts a count. For example, 4Ctrl+r redoes 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.
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:
vim:earlier 10mTo move forward five seconds from the current undo state:
vim:later 5sThe supported time units are:
- s — Seconds
- m — Minutes
- h — Hours
You can also pass a number without a unit to move by a count of changes. For example, :earlier 3 undoes three changes (same as 3u), and :later 3 redoes three changes.
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 — 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 u to 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:
~/.vimrcshset undofile set undodir=~/.vim/undodirCreate the undo directory if it does not exist:
Terminalmkdir -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 #
| Command | Description |
|---|---|
| u | Undo the last change |
| 4u | Undo the last four changes |
| U | Undo all changes on the current line |
| Ctrl+r | Redo the last undone change |
| 4Ctrl+r | Redo the last four undone changes |
| :earlier 10m | Revert to the state 10 minutes ago |
| :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 |
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 all changes made to the current line since the cursor moved to it, reverting the entire line in one step.
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.
Is there a limit to how many changes I can undo?Vim does not impose a hard limit on undo history during a session. The history is limited only by available memory. You can control the maximum number of undo levels with set undolevels=1000 (the default is 1000).
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.
Conclusion #
The u command undoes changes in Vim, and Ctrl+r redoes them. For time-based navigation, use :earlier and :later. Vim stores all changes in an undo tree, so no edit is ever lost — use g- and g+ to navigate between branches.
If you have any questions, feel free to leave a comment below.
Tags
vimLinuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
SubscribeUnsubscribe anytime. We respect your inbox.
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 pageTag » How To Undo In Putty
-
How To Undo In Vim / Vi Text Editor - NixCraft
-
How To Undo And Redo Changes In Vim / Vi - PhoenixNAP
-
How Do You Do Undo In Vi Editor In Unix?
-
Undo And Redo | Vim Tips Wiki - Fandom
-
PuTTY Keyboard Shortcuts - Defkey
-
Do We Have An Undo In Linux? - Unix Stack Exchange
-
Basic Vi Commands (Solaris Advanced User's Guide)
-
Search Code Snippets | How To Undo In Putty
-
How To Undo In Nano Editor - MonoVM
-
How To Clean Up Your Putty Sessions - Kualo Limited - MyKualo
-
Shortcuts For Putty
-
How To Undo Changes In The Nano Editor - Linux Hint
-
How To Use Undo Functionality In Nano? - Ask Ubuntu
-
Linux Terminal Undo Rm 'somefile' [duplicate] - Super User