Method of Creating Macros in VIM for Repetitive Tasks in Ubuntu 20.04:
For creating macros in VIM for repetitive tasks while using Ubuntu 20.04, you will need to perform the following steps:
1. We need to create a dummy text file for demonstrating this process. For doing that, type the following command in your terminal and then press the Enter key:
Here, replace DummyFile with the name of the file that you want to create. In this example, I have created a file named Macros.txt. This command is also shown in the following image:
2. As soon as you press the Enter key, an empty text file will appear on your screen. In this example, we are going to teach you the method of creating a macro for copying some text and pasting it multiple times in a text file. For doing that, you need to be in the Normal VIM text editor is by default in the Normal mode, so you do not need to switch to this mode. Now press “q” followed by the name of your macro. You can name your macros with small case English alphabets. In this example, the name of my macro is “m” so I will press “qm”. As soon as you press this key combination, VIM will start recording your macro, and you will be able to verify it by looking at the bottom of your screen as highlighted in the image shown below:
3. Now you need to switch to the Insert For doing that, press Esc followed by “i”. While being in the Insert mode, type anything that you later want to be copied and pasted multiple times. In this example, I want the following line to appear multiple times in my text file:
You can write anything of your choice. It is also shown in the following image:
4. Once you have typed the desired text and you have successfully recorded it, press “q” again to stop this recording. As soon as you press it, the recording status will disappear from the bottom, as shown in the image below:
5. Now your macro is all set to be played. You just need to point your cursor to the location where you want to paste the recorded text while being in the Insert mode, as shown in the image above. Then switch to the Normal mode by pressing the Esc Now type the following command:
As soon as you type it, your recorded text will automatically be played at the cursor position as shown in the following image:
6. However, if you want your macro to be played for more than once, then you need to type the following command while being in the Normal mode:
Here, replace NumberOfTimesTheMacroIsToBePlayed with an integer. In this example, I wanted my macro to be repeated 5 times, so I typed 5@q. As soon as you type this command, the recorded macro will be played at the cursor position for as many times as you have specified in your command, as shown in the image below:
Conclusion:
By following the simple method described in this article, you can create macros for repetitive tasks in VIM while using Ubuntu 20.04 very conveniently. The basics are the same for whichever type of task you want your macro to perform. However, for each different task, you will need to slightly modify the logic of your macro.
from Linux Hint https://ift.tt/2WD3Jrn
0 Comments