How To Undo And Redo In Vim / Vi - Linuxize

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.

  1. Press Esc to make sure you are in normal mode.
  2. 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.

  1. Press Esc to make sure you are in normal mode.
  2. 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 10m

To move forward five seconds from the current undo state:

vim:later 5s

The 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:

  1. Open a file and type “line one” on the first line.
  2. Press u to undo.
  3. 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/undodir

Create the undo directory if it does not exist:

Terminalmkdir -p ~/.vim/undodir

With 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 #

CommandDescription
uUndo the last change
4uUndo the last four changes
UUndo all changes on the current line
Ctrl+rRedo the last undone change
4Ctrl+rRedo the last four undone changes
:earlier 10mRevert to the state 10 minutes ago
:later 5sMove 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
:undolistShow 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

vim

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

Subscribe

Unsubscribe anytime. We respect your inbox.

About the authors

Dejan Panovski

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

Tag » How To Undo In Putty