How to Undo and Redo Changes in Vim / Vi

October 10, 2024

Introduction

Mastering the undo and redo commands in Vim is crucial for efficiently correcting mistakes and managing files. Vim's undo feature allows you to reverse unwanted changes with the u command and restore them using Ctrl + r.

This tutorial teaches how to undo and redo in Vim / Vi.

How to Undo & Redo in Vim/Vi

Prerequisites

Vim Change Tracking Explained

In Vim, change tracking allows you to keep track of edits made to a document, making it easier to review changes and undo them if necessary.

While Vim doesn't have a complex version control system, it provides basic functionality for tracking changes within an editing session through its undo and redo functions.

How to Undo Changes in Vim / Vi

Vim undoes changes by entries. An entry can be anything you do within one session in insert mode. Any changes made after pressing i (to move into insert mode) and Esc (to go back to normal mode) are considered one entry.

Also, an entry is a command you use after you press Esc. This includes deleting lines and copying and pasting text in Vim.

The following text explains how to undo changes in Vim.

Undo Last Change

To undo changes made in the last Vim entry, use u, :u, or :undo. For instance, the following text has five lines, each created in a separate entry.

Five entries in Vim

To undo changes, switch to normal mode by pressing Esc. Next, remove all changes made in the last entry with:

u
One line removed in Vim

In this example, the last line in the text is removed.

Note: Use lowercase u, not uppercase U, which undoes all changes for one line. If you type U by mistake, you can undo it with u.

Undo Multiple Changes

To undo multiple changes, press Esc to ensure you are in normal mode first. Next, specify the number of changes you want to undo in the file. To do that, add the number before the u command:

[number]u

For instance, to undo the last three changes in the example file, run:

3u
Undo three changes in Vim

Since each line was added as a single entity, 3u undoes the last three lines in the text.

Undo Latest Changes in Line

Use the capitalized U to undo all changes made within the current line since you started editing it, but only for that specific line. Unlike the regular undo command (u), which undoes the most recent change anywhere in the file, U restores the entire current line to its original state before any edits were made.

For example, the text below includes an edited version of line number five.

Edited line number five in Vim

To undo all the changes done to the fifth line, press:

U
Undo all the changes in the last line

Redo Changes in Vim / Vi

In Vim, redoing changes allows you to reverse an undo operation, effectively restoring previously reverted edits. Use the Ctrl + r command to redo the most recent undo.

For example, undo the changes made in the previous example with:

u
Undo the last change in Vim

To reverse the undo operation, press Ctrl + r:

Redo the change in Vim

Redo Multiple Changes

To redo multiple undoes, use the following:

[number] Ctrl+r

For example, to restore three previous changes, press:

3 Ctrl +r
Redo three changes in VIm

List and Access Undo Branches in Vim/Vi

In Vim, undo branches occur when you undo changes and then make a new edit, creating a separate path in the undo history.

Vim keeps track of these branches, allowing you to navigate and switch between them. To list and access these undo branches, use the :undolist command, which shows a list of recent changes and their associated timestamps.

For instance, run:

:undolist
:undolist vim command

The output you receive should be similar to the following example:

:undolist vim command output

It consists of four columns:

  • Number. The change number.
  • Changes. The number of changes made to the entity.
  • When. The time when the change was made.
  • Saved. Whether the file has been stored on disk and where.

You can then jump to a specific undo point by using the following:

:undo {number}

The number corresponds to a particular entry from the list. For example, jump to number 15 with:

:undo 15
:undo 15 vim output

To navigate through undo branches, use the :earlier and :later commands.

The :earlier command allows you to move back to an earlier point in the undo history by specifying a time or number of changes.

For example, to go back to the change from five minutes ago, type:

:earlier 5m
Undo changes from five minutes ago

Similarly, :later moves forward in the undo history to redo changes after undoing them.

Once you have finished editing the file, make sure to save the Vim file before exiting.

Conclusion

This article explained how to use the undo and redo commands in Vim / Vi. Mastering these commands improves productivity and ensures you can easily recover from errors.

To learn even more about Vim, check out this cheat sheet for Vim commands.

Was this article helpful?
YesNo
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How To Install Vim 8.2 on Ubuntu 18.04
September 29, 2022

VIM, known as the programmer's editor, is highly configurable and customizable. Also, it allows syntax...
Read more
How to Install Vim 8.2 on CentOS 7
September 10, 2019

Official repositories struggle to keep pace with updates and new software versions. This article provides...
Read more
How to Change and Use Vim Color Schemes
July 11, 2024

Vim color schemes are a handy feature for setting syntax highlighting. You can choose from a wide variety of...
Read more
How to Cut, Copy and Paste in Vim/Vi
October 3, 2024

Complete guide on how to use crucial Vim commands - copying (yanking), cutting (deleting), and pasting...
Read more