So, let’s get started with the Ubuntu 20.04 terminal shell opening, i.e., using Ctrl+Alt+T. To do some updates in vim, we must have some files with data in them. So, we have been using the “new.txt” file from our home folder of the Ubuntu 20.04 system. It contains the numerical 2-line data as per the “cat” instruction in the presented image below. Run the appended below instruction on Linux shell.
Now, you have to open this file in vim using the “vim” instruction with the filename as below. Run the appended below instruction on Linux shell.
Now, the file is opened with the shown Attention message. Press “E” to start using it. The output can be verified from the Linux shell.
The file data is shown in the normal mode of our Vim editor. In vim, we work in different modes. We have to open the normal mode first, using the “Esc” key to switch from one mode to another. We need to switch the normal mode with the insert mode to perform some updates. For that, you need to press “I” from the keyboard.
Now, we are in the “Insert” mode of the Vim editor. We have deleted the last line from this file, as shown in the attached image below.
If you want to get back the deleted line in this file, you need to use the “undo” command in Vim editor even though you have saved this file or not. To use the “undo” command, you need to open vim’s command mode. Press the “Esc” key to open the normal mode and press “:” to launch the command mode. Now, add “undo” in the command area and tap Enter.
You will see our deleted line will again show up in our file within the Vim editor. The output can be verified from the Linux shell.
If you regret performing the “undo” operation on this file, you need to perform the “redo” operation. You can use the same command mode or simply use the “Ctrl-R” while the normal mode is opened on your Vim editor. You will see that the 2nd line will again be deleted from the editor using the “redo” as the opposite of “undo.” The output can be verified from the Linux shell.
Let’s look at a similar example of “redo” changes in the Vim editor. So, we need to open the same “new.txt” text file in the Vim editor using the “Vim” command. This file will be opened in normal mode. But, if we want to add or update this file, we have to switch on its “insert” mode. Before that, switch on its normal mode by pressing the “Esc” key. After that, press “I” from the keyboard. Now the Insert mode is ON, we have added the below-shown pattern of 5 lines of data in the file. The vim editor bottom line shows that we have added 10 characters on 5 lines of the file. The output can be verified from the Linux shell.
We need to save this 5-line pattern data into the same file, “new.txt.” So, we must move back towards the normal mode first by using the “Esc” key and pressing the “:” key from the keyboard to switch on the command mode. Write “w” from the keyboard in the command area and hit the Enter key as presented at the bottom of the below image. The output can be verified from the Linux shell.
The bottom of the vim editor will show that the data is saved in the file “new.text,” i.e. “new.txt FL, 30C, written”. The output can be verified from the Linux shell.
You want to delete the changes you have just made in the vim editor by undoing it. So, you need to execute the “undo” command in the command area as presented below.
After pressing the “Enter” key, it will ask you to press the “Enter” key once again to affirm your action of “undo.” So, press Enter once again. The output can be verified from the Linux shell.
This will delete all 5 lines from the vim file you just added, as displayed below.
If you have mistakenly deleted all these lines from this file, you can redo the “undo” action. Press “Ctrl+R” or use the “redo” command in Vim’s command mode followed by the Enter key. You will see that all 5 lines will be back on your Vim file screen. The output can be verified from the Linux shell.
Finally, we have done with today’s article for using the “redo” function in the Vim editor. We have to go through the “undo” command in the Vim editor before using the “redo” command, as it’s the basic requirement of “redo” before making changes. Overall the concept of “redo” is no more complicated for Vim and Linux users when you get familiar with the Vim modes.