Vim Macros

29/12/2020
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.

  1. Open any new or existing file in vim editor.
  2. 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.
  3. Do all necessary editing tasks that will be recorded.
  4. Press q to stop the recording.
  5. 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.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Top 7 Best Paid Linux Games to Play in 2020

A decade ago, nobody would recommend Linux as an operating system for gamers. Today, Linux users can choose from an overwhelming...
29/12/2020

OpenVAS Ubuntu Installation and Tutorial

OpenVAS is the open source version of Nessus, which emerged after Nessus became a closed source scanner. Nessus was among...
28/12/2020

Mua Proxy Mỹ, Us Nuôi Tài Khoản Etsy, eBay Tìm Hiểu Về Mua Proxy Mỹ tại Onet.com.vn

Trong thời đại công nghệ phát triển như hiện nay, mua sắm trực tuyến đã trở thành một phần...
22/06/2023
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024