What is a macro

Macros allow us to repeat certain changes in Vim. In Vim, any keystroke action can be recorded and stored in a register for later playback.

To use a macro, you first record a macro, which is to type a series of commands and store the commands in a register. This sequence of commands is the macro we recorded and is what we will repeat later.

The input may be wrong or modified, so you can modify a macro directly or copy it into a document and change it.

Macros can be executed serially or in parallel, and the differences between the two methods will be explained in detail later.

In this tutorial, we’ll learn about macros by achieving a simple goal.

Suppose we have a piece of code like this:

{" foo ":" bar. 1.1 ", "foo" : "bar. 1.1", "foo" : "bar. 1.1", "foo" : "bar. 1.1", "foo" : "bar. 1.1", "foo" : "1.1" bar.}

We want to replace the first… of each line in this code with — how do we do that with a macro?

Recording macros

Recording a macro mainly uses the q command, which represents both the beginning and the end.

Start recording

The Q {reg} command is used to start recording a macro. Reg represents the register in which we want to store the macro. For example, QQ means we want to start recording a macro and save it in register Q.

Returning to the small target mentioned earlier, if we want to replace the first… on the current line of the cursor, the operation we should do is 0F.r – (0 ensures that we are following the f.. Find the same. -> first of each line), let’s store this operation in register Q:


The end of the recording

At this point, we see that the first line of. Has been replaced with -, and the macro has been recorded in register Q.

Then press Q again, and the recording ends, returning to normal mode.

The implementation of the macro

@{reg} can execute a macro in the specified register, or @@ can execute the last macro executed.

We recorded the macro in register Q, now we can use j to move the cursor to the next line and press @Q.

We have executed the previous macro, and now the first one in the second line has been replaced correctly.

To replace the following lines., just execute j@@ a few more times.

Parallel execution

Vim can also execute a macro simultaneously on multiple lines in parallel.

Let’s undo this operation and restore – back to..

Press VG to select all lines from the current line to the end, then type :normal @q, and we will see that the previously recorded macro executes simultaneously on all selected lines.

Yes, :normal @{reg} is the command to execute the macro in parallel, instructing Vim to execute the specified macro on each line of the selected area.

Macro append and edit


In the process of executing the macro serially, we found that before each execution, we had to use j to move the cursor to the next line, which was also a one-step repetition, and could also be recorded into the macro.

Just now, we recorded the macro into register Q. This time, we can use capital Q, which means we want to append to register Q. The append operation:


Go back to the text you just modified, execute @q once, and you can see that Vim also moved the cursor down one line after the substitution, which means that we didn’t break or lose the previously recorded macro, and the appended j is in effect.

Macro execution is also supported for a specified number of times. For example, execution of 5@q repeats the macro in register Q five times.

But, we may not know how many rows are left, so what do we do?

Simply put, you can type a arbitrarily large number, such as 22@q (because 2 and @ are the same key), to get the macro to execute as many times as possible.

When Vim executes macros serially, it stops execution if it encounters an error.

In the above example, when Vim gets to the last line (only one}), and cannot complete the replacement because there is no., Vim will stop executing there instead of actually executing 22 times.

The editor

What do you do if you want to do some other editing to a macro besides appending it?

Earlier we recorded the macro into a register, the same register that we used for copying, deleting, and so on. When we record a macro, Vim stores the sequence of actions we recorded in a register as a piece of text.

To edit a macro, the basic idea is to paste the text in this register, edit it, and then save it back into the register.

Take the macro just recorded into register Q as an example:

  • with"qpPut the contents of register Q out
  • Edit the copied text
  • with"qyyCopy this line into register Q
  • Delete this line of textdd(because it is no longer useful)

This can also be done by editing directly from the command line:

  • The input:let @a='
  • According to the<ctrl-r>aThe registerqThe text in the macro we just recorded is inserted
  • Edit the copied text
  • Make up what’s missing'Then press Enter

You probably don’t need macros

Going back to the goal, this example is just to demonstrate the use of macros, but if you’ve delved into Vim’s visual mode, you’ll notice that you don’t need macros to do this, recording, executing, etc.

Press < Ctrl-v > in Vim to enter “block viewable mode”, which allows you to edit multiple lines of text at the same time. Take this example:

  • f.Find the character you want to modify.
  • <ctrl-v>jjjjjSelect the row you want to modify
  • r-Complete replacement

As you can see, each row that is selected has only the first one. It is replaced instead of the whole row being selected as in normal visual mode.