How to Undo and Redo in Vim?

In Vim, anything you enter in the insert Mode before returning to the Normal mode is a change. The commands you use in the Normal mode change the data in Vim. We can Undo and Redo the changes in Vim, which allows reversing the changes or getting back the previous changes. This guide will discuss the shortcut keys and commands for Undo and Redo in Vim.

Let’s start!

The article contents are as follows:

Let’s start discussing the content of the article in the next section.

How to Undo in Vim?

How to install Kali Linux in Window...

The Undo command is useful in Vim to get the previous content after reversing the changes.

The shortcut keys or commands to Undo the changes in Vim are shown below:

u :u :undoTo Undo the last change, use any options.
[number] u
 
:undo [number]
To Undo the multiple last changes, we can put the number in this common. Like: to undo the last two tasks, use the “2u” or “:undo 2” commands.
UTo Undo all the changes on a single file.
:undolistTo list all the changes that are undone.

In the next section, we will tell you how to undo a file in Vim and all the relevant steps.

How to Undo a File in Vim?

We can easily delete a file in Vim. The steps for Undo in Vim are as follows:

Note: The steps to Undo and Redo are the same, but the commands are different for different uses.

Step 1: Launch the Vim editor

Open the desired file (in my case: testfile2) in the Vim editor by executing the following command in the terminal:

$ vim <testfile2>

Step 2: Switch to Insert mode to Edit File

To edit a file or change the file’s content, we must press the “i” key to change to the “insert” mode:

Step 3: Switch to Normal mode

To change the mode of Vim to “Normal”, press the Escape (Esc) button:

Step 4: Undo the Change

Press the “Esc” button to switch to normal mode from insert mode. To “Redo” the change in the file, press the “u” key or enter “:undo”:

Caution: The lowercase “u” shortcut key should be used; otherwise, the uppercase “U” will redo all the previous changes in that line.

Note: The commands & shortcut keys will be entered in the “Normal” mode of the Vim editor.

How to Undo Several Changes in Vim?

The steps to Undo are the same as discussed above, but the commands are different. To undo several changes at a time, we can use the “[number]u”, or “:undo [number]” commands by replacing the “number” with the desired number of changes.

To Undo the last three (3) changes in Vim, we can utilize the “3u” command in Vim, as follows:

How to Get a List of the Previous Undo?

The changes in the system are like a tree structure. You undo a file and then make new changes in the file. The previous undo change becomes a branch for the new changes. The change number for the previous changes keeps on increasing with more changes.

To get a list of undo, we can use the “:undolist” command in Vim.

:undolist

The “number” is the change number that will increase with more changes.

The “changes” column shows the level of the change from the root directory.

The “when” is the time and date when the undo change has occurred.

The “saved” column shows whether the changes were saved in the system.

The above output shows a change number, which will increase with more changes and creates a tree structure with smaller change numbers for previous changes.

How to Redo in Vim?

The Redo command allows you to get back the changes you have Undone. To Redo a file in Vim, use the below-mentioned commands:

Ctrl + r.This shortcut is used to redo a change in Vim.
:redoTo Redo a change in Vim.
[number] ctrl + rTo Redo the several undo changes.

How to Redo a File in Vim?

To redo a file in Vim, the first three steps are the same as in undo method. The following steps are carried out to redo a file in vim:

  • Step 1: Launch the Vim editor.
  • Step 2: Switch to Insert Mode and Edit the text
  • Step 3: Switch to Normal Mode
  • Step 4: Redo the Change

To redo the change, use the shortcut key “Ctrl + r” or insert the “:redo” command in normal mode:

:redo

How to Redo Last Several Changes in Vim?

To Redo the last few specified changes in the Vim editor, follow the above steps and use the below general syntax:

[number] ctrl + r

To redo the last two (2) changes in Vim, press the “2” key and use the shortcut “ctrl + r”.

How to get the Total Changes in Vim?

You can get the number of recent changes in the file from when the file was opened in the Vim editor. To get the recent change number, use the below “changenr” command:

:echo changenr()

That’s the end!

Conclusion

To undo in Vim, use the “u”, “:u”, or “:undo” commands, and to redo “:r” or “ctrl + r” commands are utilized in “Normal” mode. To undo multiple changes in Vim, utilize the “:undo [number]”, “[number] u”, and “[number] ctrl + r” commands for redo. This post has provided a detailed guide to redo and undo in vim.