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 [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.

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 5 Image Editors for Linux

Let’s be honest – as much as it is a powerhouse for any sort of technical development, scripting and whatever you can...
28/12/2020

Cách sử dụng proxy để truy cập vào các trang web bị chặn.

Trong những năm gần đây, vấn đề bị chặn truy cập vào các trang web đã trở thành một vấn đề...
27/02/2023

Upgrade To Latest SQLite3 on CentOS7

How to Upgrade to the Latest SQLite3 on CentOS7 SQLite 3 is a lightweight simple file based database system. It is used...
28/12/2020
Bài Viết

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

Lý do tại sao bạn nên sử dụng proxy khi truy cập web đen
27/02/2023

Các lỗi thường gặp khi sử dụng proxy và cách khắc phục chúng.
27/02/2023

Tác động của việc sử dụng proxy đến tốc độ kết nối internet của bạn.
27/02/2023

Các tiện ích và công cụ để quản lý proxy.
27/02/2023

Các cách để kiểm tra tốc độ và độ ổn định của proxy.
27/02/2023