A detailed example of using macro commands in Vim
- 2020-06-19 12:33:46
- OfStack
preface
The design philosophy of Vim includes the following sentence: "if you a thing once, it is okay. then you find better way to do it ".
Macro of Vim is designed to solve the problem of duplication. macro is covered in the Vim register article. macro operates in registers as text.
Macro is a collection of 1 set of commands, which are widely used, including word editor in MS Office, excel editor and various text editors. VIM, the most powerful text editor in Linux system, also supports macro operation. This section briefly explains 1 macro is used in VIM
q* starts recording macros q stops recording macros @ * performs macro @@ repeats the @* command last time onceThe above commands are used under VIM's general mode 1 (Normal), where * represents a number and word character, and the regular expression rule is: [0-9ES55en-ES56en-ES57en]
:help recording View macro helpSuch as in 1 a mode input qq will see recording vim below the status bar, said it had started recording macros to register q q may also be a macro id, at this time of the command will be recorded in the macro, again in 1 a mode press q namely stop recording macros, and can call by @ q behind the macro
Simple to use
Record macro using one of the 26 letters of q + [ES76en-ES77en]
q[a-z]
All subsequent commands are recorded, and at the end press 1 q.
When executing macro, add @ to the register, such as in register a
10@a
Execute the command recorded in the a register 10 times.
@@
Execute the previous command one more time.
Edit macro
Assume that there is already one macro stored in a and ready to use
:let @a=' Enter Ctrl + r + a to insert a. Edit the content and then exit with 'end EnterCheck the macro
macro content is saved in a and can be viewed directly using :reg a.
For example,
Classic comments and tail additions
Comment, or add a specific character at the end of each line, such as a semicolon at the end of each line; , there are too many ways to do this in Vim, such as replacing :%s/$/; /g, such as the. Command, and macro to be used here:
int a = 1
int b = 2
int c = a+b
print a
print b
print c
If you do it with., first execute A in line 1. , and then repeat j. 5 times. This is easy to use for a simple file like this, but if the file is 1,000 lines long, then obviously the. Command won't work. With macro, you can record it once and then execute it on 1000 lines.
For example, qaA can be used in normal mode; < Esc > jq
qa starts recording and stores in the a register A enters insert mode at the end of the line ; Insert a semicolon < Esc > Exit insert mode j 1 row q exits recordingAt this point, the operation of the current row is saved in the a register and is added to the current row. And move the cursor to the next line.
After recording, you can use @a
1000@a
Execute macro 1000 times to add the end of the following 1000 lines;
Increasing Numbers
You can use macro to insert 1 to 100 Numbers, adding 1 for each row:
1
2
3
...
100
First, insert 1 at line 1, then position the cursor at "1" to enter normal mode
Type 1
qayyp<Ctrl>aq
yyp copy 1 line and paste it on a new 1 line,
<
Ctrl
>
a number + 1
q finished recording
The last execution
98@a
conclusion