vim

Vim Undo and Redo howto

There are many situations where you have to remove the changes you have made to a text file, or you need to get back the update a few steps earlier. “Undo” means to get back to the last state of something while ‘redo” is used to undo the last “undo”. In computer systems, we usually use “Ctrl-Z” to undo any change performed within editors like notepad and Microsoft word. In the Vim editor of Linux, we can also undo our changes using the Undo operation but with different key instructions. This guide is going to explain the methods to perform “undo” and “redo” in the Linux Mint operating system.

You need to make sure that the “Vim” editor has already been installed and configured in your Linux mint system. After installing the Vim editor, you can now open your newly generated text file in it via the following query:

omar@omar-VirtualBox:~$ vim new.txt

Example # 01: Undo and Redo Once Via “u” and “Ctrl-r” Key

The new.txt file has been launched in the normal mode of the “Vim” editor. You can switch back to normal mode in the Vim editor using the Esc key but you cannot type or insert anything. Therefore, we need to switch into its insert mode by pressing the small “I” from the keyword while you are still in the normal mode. The mode of the Vim editor will be shifted to “Insert”.

We have inserted the shown 3 lines in total within the file by utilizing the “insert” mode of the Vim editor.

To permanently write and save the changes we have made to this file, we need to use the normal mode once again. Therefore, press the “Esc” key to move back to the normal mode. The “Vim” editor utilizes the “w” key to save the changes. So, use “:” and “w” together in the normal mode as it is also displayed at the bottom of the file opened in the Vim editor. Press Enter after “:w”.

The 3 lines will be saved into the file permanently as per the output shown at the bottom of the file: “new.txt” 3L, 53B written.

Now, to undo the last change we have made to the new.txt file, we need to tap the “u” key from the normal mode and you will see that the last update will be reversed. The output of the file “new.txt” opened in the Vim shows that the inserted lines are removed.

Now that we have tried “undo” to clear the new.txt file, we will be trying “redo”. To redo the changes made by “undo”, we moved back to the normal mode using the “Esc” key and pressed “Ctrl-r”. The text we added to the file came back in a second as per the output shown in the attached photo. This is how we perform a “redo” operation for a single change in Linux.

Example # 02: Undo and Redo Once Via “:u” and “Ctrl-r” Key

Within the illustration above, we have tried the “u” key to undo the last action we have made. Now, we will be using another method to undo the changes. Therefore, we are using the last updated text file from the example above: a total of three lines in it. Press “I” in its normal mode to get into the insert mode. Now, we have removed the last line of text from the “new.txt” as shown in the insert mode of the attached photo.

After removing the last line, we entered the “normal” mode of Vim using the “Esc” key and pressed “:” to open its command mode. After pressing “:”, you have to press “w” to write the changes.

To try the “undo” operator in the command mode, we need to use “:u” in the normal mode. The illustration of “:u” in its command mode has been shown in the image before its execution.

Now, to execute the “:u” command in the command mode, press Enter. The Vim terminal will confirm your action by asking you to press Enter once again: “Press Enter or type command to continue”. So, press the Enter key again for the execution of this query.

The last line we removed came back in a second using “:u”.

To redo the “undo” action we have just made, press Ctrl-r in normal mode. See, the last line has been gone again which performed “redo”.

Example # 03: Multiple Undo and Redos at Once

In this example, we will undo multiple changes at once and redo the multiple “undo’s” as well. Let us say, you have an empty file “test.txt” in the Linux mint system and you have opened it in the Vim editor using the query shown below.

omar@omar-VirtualBox:~$ vim new.txt

Open the insert mode and add a single line. Save the update using the “:w” command in normal mode.

Add the second line and save it using the same procedure mentioned in the paragraph above.

Add 3rd line the same way and save it.

Now, we just made 3 changes to the empty file. To get back the empty file, we need to perform 3 “undo’s”. To perform 3 “undo’s” at once, we will try the “3u” command in the normal mode.

After getting back the empty file after undoing the last three changes, we undo the “undo” operation. For this, we tried the “Ctrl+r” sequence of keys in the normal mode and have the 3 line text data back in the file which is the last three combined “undo’s”.

Conclusion

After going through this guide, you will have enough knowledge of using “Undo” and “Redo” operations in the Linux Mint “Vim” editor. There is a brief explanation regarding the “undo” and “redo” operations. While the first and second illustration covers the use of both operations for a single change. The last example is discussing the way of performing multiple “Undo” and “Redo” operations at once without executing the “undo” and “redo” command more than once.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.