vim

How to create Macros in VIM for Repetitive Tasks?

A macro is defined as a process that specifies the execution sequence of a certain operation. Macros are generally created for the tasks that are supposed to occur quite frequently. In Windows operating system, Microsoft Word also allows you to create macros for editing purposes. Similarly, in the Linux operating system, the VIM text editor provides you with the ability to create macros very easily. Therefore, in this article, we will explain to you the method of creating macros in VIM for repetitive tasks while using Ubuntu 20.04.

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:

vim DummyFile.txt

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:

Creating VIM Macros for Repetitive Tasks

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:

=@q

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:

NumberOfTimesTheMacroIsToBePlayed@q

Here, replace NumberOfTimesTheMacroIsToBePlayed with an integer. In this example, I wanted my macro to be repeated 5 times, so I typed [email protected]. 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.

About the author

Karim Buzdar

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.