Example
In the text with only one line and only one line, type the following command:
qaYp<C-a>q
→
qa
Start recording
Yp
Copy rows.
<C-a>
Add 1.
q
Stop recording.
@a
→ Write down 2 below 1
@@
→ Write 3 in front of 2
- Now do
100@@
A new 100 row is created and the data is increased to 103.
In all these commands, I did not understand what Ctrl + A meant. Although it is easy to guess, I google it, in vim, CTRL + A and CTRL + X are also useful, that is, adding numbers + 1 and-1. So I imitated the macro recording above to implement a {1, 3, 5, 7, 9, 11 ......} Such a data list. 1. enter 1, (1, "Space-for encoding) 2. input QA (Start recording) 3.yyp( copy the current row and paste it to the next row) 4.2, CTRL + a (enter 2 first, then press Ctrl + a) 5.q (Press Q to End recording) 6. N @ (this command is used to replay the macro, N, which indicates the number of times to be repeated) 7. select the data you just generated and repost the following information by J (concatenate all rows into one row) from: the http://www.cnblogs.com/ini_always/archive/2011/09/21/2184446.html is about recording macros into different registers, QA (A in a refers to register a, which can also be B and C Shenma, and register 5 @ A is also used, 5 refers to the use of the macro recorded recently)
When editing a file, you may need to perform a specific operation many times. Take editing the following file as an example:
;=====================================================================================;This is a sample configuration file when upgrading XXX using InstallShield.;Author: ini_always;Date: 8/24/2011;Last modified: 9/20/2011;Note: Install script does NOT verify whether the configuration file is in a "WELL";format, a WRONG format may lead to installation failure.;If more information is needed, please check the document for details.;=====================================================================================
Copy code
This is an ini configuration file. You can see a comma at the beginning of each line. What should I do if I need to remove the comma before each line? Press X at the beginning of the first line, J, and then X? Indeed, this is also the case at the beginning, but what if there are 100 lines of this file to be modified like this? Or 1000 rows?
Okay, let's get started. Macro refers to a series of operations in a specific sequence in Vim. We can record our operation sequence and repeat the sequence multiple times to simplify some repeated operations. The Vim macro has the recording and playing processes. Recording is the process that you teach Vim how to operate, and playing is the process that Vim automatically operates according to what you teach. Therefore, for the above file processing, you must first perform macro recording:
1. Position the cursor in the first line;
2. in normal mode, enter QA (of course, you can also enter QB, QC, etc. Here A, B, and C refer to the Register name. Vim will put the recorded macro in this register) (PS: if you do not know what the vim register is, please search for it by yourself );
3. under normal circumstances, the command line of VIM displays the "Start recording". At this time, move the cursor to the first character (press 0 or |) and then press X to delete it, press J to jump to the next line;
4. Input Q in normal mode to end macro recording.
Well, after the above steps, we define a macro stored in register a. Its operation sequence is: 0-> X-> J, that is, jump to the beginning of the line, delete, jump to the next row.
Now, the comma at the beginning of the line has been deleted in the first line, and the cursor is already in the second line. Now, in normal mode, enter @, to play the macro in register a that we just recorded. As a result, the comma at the beginning of the second line is also deleted, and the cursor stops at the third line.
Isn't that easy? You will definitely think like this: to delete 100 rows, I have to enter 100 @ A. I 'd better delete them manually. Well, VIM has long come to mind. Input 7 @ A, okay, and the remaining 7 rows are all done. (PS: Add a number before the command to indicate how many times the command will be executed)
Of course, this example is very simple, but it is also very typical. Using Vim macros can make some boring work much easier.
Macro recording is so powerful that we can do a lot of repetitive work for us. This article is to be supplemented.