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 [email protected] 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 [email protected] 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 [email protected] 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.
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.