Introduction

Knowing how to redo is a fundamental skill every Vim / Vi user should have. No matter the text editor, the possibility to undo and redo changes is an essential feature.

In this tutorial, learn how to undo and redo in Vim / Vi with examples of commands.

tutorial on undoing and redoing changes in vi / vim

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 can be a command you use after you pressed Esc. This includes commands like deleting lines and copying and pasting text in Vim.

Generally, any time you switch from one mode to another, a new entry is created.

Undo Last Change

1. First, switch to normal mode by pressing Esc.

2. Then, undo changes made in the last entry using one of the following commands: u, :u, :undo.

3. All changes and additions made in the previous entry will be removed.

For example, in the file you see below, each line represents a separate entity. By running the :undo command the last line in the text will be removed.

Command to undo the last Vim Vi change

Once you hit Enter, the last line is removed and the output tells you there is 1 line less.

example of undoing changes in Vim


Note: Apart from telling you how many lines were modified, Vim also notifies you of the number of these lines. Therefore, showing Vim line numbers may help you to follow these changes and keep track of what has been undone.


Undo Multiple Changes

1. Press Esc to ensure you are in normal (command) mode before running any Vim commands.

2. For multiple undoes at once, you need to specify the number of changes you want to undo in the file. This is done by adding the number before the u command following the syntax: [number]u.

To illustrate, in our sample Vim file we ran the command:

4u

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

Undoing multiple changes in a single command inside vim editor

The output should indicate there are 4 fewer lines.

Undo Latest Changes in Line

1. Move to command mode with Esc.

2. Undo the latest changes within one line with the command U. Make sure it is capitalized.

In our example, the command undoes the changes made within the last line where the last sentence was added in a subsequent entry.

Undo last changes in line.

Redo Changes in Vim / Vi

1. To redo in Vim, you need to be in the normal mode (press Esc).

2. Now you can redo changes you have previously undone – hold Ctrl and press r.

Vim will redo the last undone entry.

Redo Multiple Changes

1. Return to command mode with Esc.

2. Redo multiple undoes with a single command using the syntax: [number]Ctrl+r. You need to write the number of redoes you want to preform, press and hold Ctrl and type r.

List Undo Branches

While working on a file in Vim / Vi, you will most likely undo a change and then write over it by adding new changes. This undone change does not disappear completely. In fact, it becomes a branch that you can find and access later on.

To display undo branches run the command:

:undolist

The output you receive should be similar to our example:

Listing of undo branches in Vim

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.

Conclusion

After reading this article, you should understand how to use the undo and redo commands in Vim / Vi. Remember, yo undo a change in Vim / Vi use the command u, and to redo a change which was undone use Ctrl-R.

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


Next you should also read