Vim Macros

Vim is not just an editing tool to create or modify any types of files. It has many useful and powerful features that help to make many tasks easier and faster. One of the helpful features of vim is Macros. The editor can edit the text of a file more efficiently than other editors by using this feature. The editing task can be recorded and played for multiple times in a file by using macros. Repetitive editing task can be done quickly by recording steps and applying in the file with macros. It runs text as a sequence of commands. How you can use macros in your vim editor is shown in this tutorial by using different types of recording examples.

Record and play macros

How macro works in the vim editor is described in the following steps.

Open any new or existing file in vim editor.

Start recording by pressing q and macro name. Define macro name by any lowercase character from the range, a-z. If the macro name is b then press Esc and qb to start recording.

Do all necessary editing tasks that will be recorded.

Press q to stop the recording.

Press @ symbol followed by macro name to run the macro for one time and press the number of times followed by @ and macro name to play the recording for multiple times.

Example -1: Using macro to generate the same text multiple times

Open a new file named file1.txt in the vim editor to do the simple copy paste task by using macro.

Press q followed by z to start the recording. Here, z is the macro name. If the recording starts properly then ‘recording @z’ will be displayed in the bottom of the editor like the following image.

Press i and type any text that you want to repeat. Here, “Learning Vim Macros” is entered as text.

Press q to stop the recording after adding text in the file. ‘recording @z’ message will be disappeared from the bottom after stopping the recording. Now, macro is ready to play.

If you want to run the previously created macros for one time, then go to the desired location of the file and press @z. This will play the recording for one time.

If you want to play the recording for 4 times then press 4@z. This time macro will play for 4 times and the text will print for 4 times like the following image.

Example -2: Using macro to generate sequential number

Suppose, you want to generate a series of numbers from 101-110. This task can be done easily by using macro. Open a new file named file2.txt in the vim editor. Press i and type 101.

Press ESC and qy to start the recording with macro name, y. Go to the starting of the first line and press yy to copy the text and press p to paste the text in the next line.

Press Ctrl+a to increment the value of the second line from 101 to 102. Now, stop the recording by pressing q like before. Type, ESC following by 8@y to the macro, y for 8 times and generate the number from 103 to 110 like the following image. In this way, you can create a large series of number by using macro.

Example – 3: Adding the same text multiple times in each line of a file

Suppose, you have a text file named file3.txt that contains the item data and you have a database table named items where you want to insert the data of file3.txt. Data can be inserted in any database by executing INSERT query. So, if the content of file3.txt can be modified properly with INSERT statement then the insertion task can be done faster. This type of modification can be done more easily by using macro. Open the file in vim editor and press ESC followed by qd to start the recording with the macro name, d.

Go to the starting of the file and press i to insert text. Type, “insert into items values(“ as a part of INSERT query. Press ESC, w and yw for 5 times. Here, w is used to go to the next word and yw is used to copy the current word. There are five parts in each line. These are ID (Ex-11001), ‘,’, Name(Ex-HDD),’,’ and Brand(Ex-Samsung). Press ESC and a to go the end of the line. Type, “);” as last part of INSERT query.

Now, press j followed by q. Here, j is used to move to the next line and q is used to stop the recording. There are four lines of text in the file and it each line needs to be modified like the first line that is shown in the previous image to insert the data in the database. Run the macros for 3 times by pressing 3@d to modify the remaining lines with INSERT statement. The following output will appear if the macro is recorded and played properly. Now, you can easily copy the text of this file and paste in any database software to execute the insert queries and insert 4 records in items table. Using macro is beneficial if you want to do this type of task for modifying a large list of data.

Conclusion

This tutorial shows how vim macros can make our editorial tasks easier than before. The users can save their time and effort by creating and applying different types of macros and completing repetitive tasks just in a moment. Macros are mainly used for faster transformation. If you want any task permanently or for long times, then it is better to use a function instead of macro.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.