When editing a file, there may be situations where a particular action needs to be done many times to edit the following file as an example:
;=====================================================================================; this isA sample configuration file when upgrading XXXusingInstallShield.; Author:ini_always;date:8/ -/ .; Last modified:9/ -/ .; Note:install script does not verify whether the configuration file is inchA" Well"; format, a wrong format may leads to installation Failure.;i F More Information isneeded, please check the document fordetails.;=====================================================================================
This is an INI-type configuration file, you can see that each line is preceded by a comma, now if you need to remove the comma in front of each line, what should I do? Press X at the beginning of the first line, then press J, then press X ... Repeat this? Yes, I did, but what if the file has 100 lines to change? or 1000 lines?
Okay, cut the crap and get to the chase. The so-called macro, in Vim refers to a sequence of operations in a particular order, we can record their own sequence of operations, and then repeat the sequence multiple times to simplify some kind of repetitive operation. VIM macro has the process of recording and playing, recording is what you teach Vim to do, play is vim according to you teach automatic operation. Therefore, for the above file processing, the macro recording is first:
1. Position the cursor in the first row;
2. Enter QA in normal mode (you can also enter QB, QC, etc, here A, B, c refers to the register name, VIM will put the recorded macro in this register) (PS: If you do not know what is the VIM register, please put the dog search);
3. Normally, Vim's command line displays the word "Start recording", at this time, position the cursor to the first character (press 0 or |), then press X to delete, press J to jump to the next line;
Enter Q in 4.normal mode to end the macro recording.
Well, after the above steps, we define a macro stored in register a, whose sequence of operations is: 0->x->j, that is, jump to the beginning of the line, delete, jump to the next row.
Now that the first row has deleted the comma at the beginning of the line, and the cursor is already on the second line, now enter @a in normal mode to play the macro we just recorded in register a. As a result, the comma at the beginning of the second line is deleted, and the cursor stops in the third row.
That's not easy, is it? You must think so, to delete 100 lines, I have to enter 100 @a, I might as well manually delete it. Oh, vim has long thought, input [email protected], OK, the remaining 7 lines are all done. (PS: Add a number to the front of the command, which means how many times to execute the command)
Of course, this example is very simple, but it is also typical. Using the good vim of the macro, you can make some of the original boring work to be much simpler.
Vim repeat Action macro recording