At work, we often have such requirements for editing text files:
- Enter the same text multiple times.
- Generates a serialized text.
- Each line of text inserts a line of the same text.
In addition, there are a lot of repetitive actions. For these requirements, if we manually operate, it must be very boring, and waste a lot of time and energy.
This is where the Vim macro comes in. It can quickly do these repetitive tasks in a few simple steps, saving time and effort! I’ll explain it in detail.
Macro recording and running
Macro recording
- Create a new file or open an existing file in Vim editor;
- According to the
q
And keep up withMacro name
Start recording. Let’s say we want to name our macrob
And then theEsc
和qb
Start recording; - Text editing ing…
- According to the
q
Stop recording. At this point, the recording of macro is complete.
The operation of the macro
Press @ macro name to run the macro once, and press times to run the macro repeatedly.
Examples are given below to explain in detail.
Example -1: Use macros to generate the same text multiple times
For example, if we want to generate 100 lines of the same text, a clumsy way is to copy and paste the number of times needed. This is very inefficient and error prone. With macros, everything is super simple!
First, we create a new file named file1.txt in the Vim editor.
Then, we press Q and then Z to start recording macros. So z is the macro name, and we’ll use it later. If it’s recorded correctly, the words record@Z appear at the bottom.
After that, press I and enter the text you want to repeat. For example, enter the text Learning Vim Macros here.
Finally, press Q to stop recording, and record@z will disappear from the bottom. This completes the recording of a macro called Z, which we can use to repeat the text entry later.
If you want to run the macro you just created once, move the cursor to the desired location in the file and press @z, and the text will be copied.
If you want to copy this line N times, you can run the macro multiple times. All you need to do is add the number before the macro. For example, 4@z can run the macro four times, and the corresponding text will be copied four times.
Example 2: Using macros to generate serial numbers
In example -1, we mechanically duplicate a bunch of the same text, so what if we want to generate text with sequences? It can also be done easily using macros!
Suppose we want to generate a series of numbers from 101 to 110. Let’s do this together.
First, create a new file called file2.txt in the Vim editor, then press I and type 101.
Press ESC to exit typing. Then press QY to record subsequent operations with the macro name Y. We move the cursor to the beginning of the first line and press YY to copy the text, then press P to paste the text into the next line.
Next, press Ctrl + A to increase the value of the second line from 101 to 102. Finally, press Q as before to stop recording and our macro is finished.
Next, it’s time to witness the miracle!
We type 8@y, and the macro we just recorded runs eight times, generating consecutive numbers from 103 to 110. One key operation, simple and efficient!
This is very useful for sequential text, so you don’t have to repeat the tedious task of copying –> changing numbers — and your efficiency will be greatly improved!
Example -3: Add the same text to each line of the file
When we’re working with databases, there are a lot of situations where very similar statements, such as insert into Items values(100, 20077100050, alvin), are repeated many times. If we now have data to insert, how do we quickly generate multiple SQL statements?
In this example, we assume that the data file file3.txt has four pieces of data, as shown below.
First, we open the data file file3.txt in the Vim editor, then press ESC, then QD to start recording with the macro name D.
Move the cursor to the beginning of the file and press I to insert text. Then type INSERT into items values(, then press ESC, w, yw 5 times.
Here, w is used to go to the next word and yw is used to copy the current word. And the reason why we have 5 times here is because we have 5 parts in each row. They are 11001, HDD, Samsung (note that commas count as part).
After that, press ESC and A to move the cursor to the end of the line and type); .
Finally, press J and then Q. Here, j is used to move to the next line and Q is used to stop recording. Now we have all the macros we need.
Now, we want the remaining three lines of text to be formatted as the first line, just by pressing 3@d to run the macro three times. This way, it is much easier to insert into the database, and we do not have to manually change each one. Simple and efficient!
conclusion
When we write code, there are many occasions when the text and code are highly similar, if we manually modify one by one, it will be very inefficient. With Vim macros, we can save time and effort by creating and using different types of macros to do these repetitive tasks.
All you see is true love. Why don’t you like it before you leave? Your “sanlian” is the biggest motivation for liang Xu’s continuous creation!
- Pay attention to the original public number “good Xu Linux”, the first time to get the latest Linux dry goods!
- Public number background reply [information] [interview] [resume] to get selected first-line big factory interview, self-improvement, resume and other information.
- Follow my blog: lxLinux.net