Undo and Redo in Vim

Vim is a versatile text editor with advanced options for developers and is known for its shorthand keys and commands. You can quickly perform operations like undo or redo using a single command. The undo operation is carried out to revert to the specific change that occurred previously. Similarly, if you have performed “undo” to revert to some last version, you can “redo” it again to restore the state as it was before applying the undo operation.

In our today’s Vim guide, we will provide the possible ways to undo and redo in Vim. 


Note: Ensure that you have not quit the editor after the changes. If you have quit the Vim after saving, Vim Undo./Redo won’t work.

Understanding the Undo and Redo in Vim

Any change or edit in the Vim is recorded in Vim’s tree. All the changes are recorded in a sequential manner.

Look at the following image, “Version 1” has a total of three changes. Here’s a quick explanation of the Undo and Redo using the Vim tree structure as shown in the image:

  • Undo: Moving from “Change 3” to “Change 2”, and then to “Change 1”.
  • Redo: Moving from “Change 1” to “Change 2”, and then to “Change 3”.
Undo And Redo In Vim 1

Keeping the figure in view, here is how the “undo” and “redo” actually work:

  • Undo: If you are at the oldest change (Change 1), the undo won’t work further.
  • Redo: Similarly, If you are already at the latest change (Change 3), you won’t be able to Redo further.

Moreover, the “undo” and “redo” are applicable in the current session. If you have saved and exited the file, you won’t be able to use Undo and Redo in Vim.

How to Undo in Vim

The purpose of the undo operation is to correct any mistake that occurred in the previously performed actions using Vim. Let’s practice the supported commands to undo:

  1. u or :u Command

Run the following command in the command line mode to undo the last change:


I have commented the line “Defaults….”, highlighted below:

Undo And Redo In Vim 2

Upon execution, you can see the line is uncommented and the Vim editor has confirmed the “Undo” action, as seen below:

Undo And Redo In Vim 3

Similarly, you can prepend a digit before the “u” to undo the specific number of changes:

:<number>uRevert the changes on the specific lineThe “:7u” command will undo the changes done at line number 7.
:u <number>Revert all the specified changes.The “:u 4” will undo the last four changes that occurred in the file.
  1. U Command

Moreover, you can use the “U” (in capital) to undo all the changes on the current line. Here is the line where several changes were attempted:

Undo And Redo In Vim 4

Now, when the U command is applied, all the changes on that line are undone, as can be seen below:

Undo And Redo In Vim 5
  1. :undo Command

The undo command also reverts the last change applied. Here is the basic command syntax to use the “undo” command:


Here is the screenshot before the change:

Undo And Redo In Vim 6

Upon undoing, the last change will be reverted as can be seen in the below screenshot:

Undo And Redo In Vim 7

The “undo” command provides more control over the undo operation as compared to the simple “u” command. Here is the further extended usage of the “:undo

:<number>undoUndo at Specific LineSpecify the number to undo the changes in that specific line. For instance, if we use the “2” in place of “number”, the change on “line number 2” will be reverted.
:undo numberUndo the Specific number of changesLike, if we use 4 as a number then the last four changes will be reverted. 

For instance, the following command will undo the last three changes:

  1. :earlier Command

The “earlier” command reverts the changes that were done before the specific time. The time could be in minutes or hours. For instance, the command below reverts the changes done before “10mins”:

:earlier 10m

Let’s apply it:


Undo And Redo In Vim 8


You can see there are a total of 8 changes have occurred that occurred in the last 10mins:

Undo And Redo In Vim 9

Similarly, you can specify the time in “hours” (:earlier 5h) and “seconds” (:earlier 5s) to restore the file version at a specific time.  

How to Redo in Vim

Redo is the opposite of the undo. The redo operations undid the last change. Redo operation is carried out using one of the below commands/shortcuts:

:red Command

The “:red” command refers to the redo operation. Here is the 

The undo was applied which resulted in uncommenting the line. Here we will apply the “redo” operation:

Undo And Redo In Vim 10

After applying the “redo” operation, the recent undo was reverted and hence resulted in commenting on the above-highlighted line again:

Undo And Redo In Vim 11

:later Command

Like the earlier command, the “later” command refers to the redo operation which actually redoes the changes that were undone in the specific time, i.e., in the last 5 minutes, last 1 hour, or 10 sec. 

For instance, the below command redoes the undid changes in the last 7mins:

:later 7m

Here is the snapshot of the file before applying the above command:

Undo And Redo In Vim 12

After applying the command, the file turns out like this:

Undo And Redo In Vim 13

CTRL+r Shortcut

The “CTRL+r” shortcut key in the normal mode will redo the last change that was undone. Here are the shortcuts that you can use to redo the changes:

To Redo the last Change:


To Redo Multiple Changes: Press any digit with the CTRL+R to Redo the specific number of undid changes:


To “Redo” multiple times, you need to keep on pressing the “CTRL+r” again and again. 

Bottom Line

Let’s summarize. If you want to undo the change(s) in Vim, you can use “:u”, “:undo”, or the “:earlier” commands. And if you want to redo the changes (undid), use the commands, “:red”, “:later”, and “CTRL+r”.

Go through this guide to learn the detailed functionality of all the commands/shortcuts supported to undo and redo in Vim.