What is a macro

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

To use macros, you first record a macro, that is, enter a series of commands, and also store the series of commands in a register. These commands are the macros we recorded and the actions we will repeat later.

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

Macros are executed serially and in parallel, and we’ll explain the differences later.

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

Suppose we have code like this:

{
  "foo": "1.1 bar."."foo": "1.1 bar."."foo": "1.1 bar."."foo": "1.1 bar."."foo": "1.1 bar."."foo": "1.1 bar."
}
Copy the code

We want to replace the first. In every line of this code with -. How do we do that with macros?

Recording macros

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

Start recording

Use q{reg} 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 guarantees that our f. Find the same. -> first of each line), let’s store this operation in register Q:

qq0f.r-
Copy the code

The end of the recording

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

Then press Q again to end the recording and return to normal mode.

The implementation of the macro

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

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

We executed the previous macro, and now the first. In the second line is replaced correctly.

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

Parallel execution

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

Undo the previous operation and restore the – back to.

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

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

Append and edit macros

additional

In the previous serial macro execution, we noticed that before each execution, we had to use j to move the cursor to the next line, which is also a repeat operation, and can also be recorded in the macro.

Just now, we recorded the macro in register Q. This time, we can use capital Q, which means we want to append to register Q. Appended operations:

qQjq
Copy the code

Go back to the text you were modifying, execute @q once, and you can see that Vim also moves the cursor down one line after the substitution, indicating that we haven’t broken or lost the previously recorded macro, and that the append j is working.

Macros can also be executed a specified number of times; for example, the macro in register Q can be repeated five times by executing 5@q.

But what if we don’t know how many rows there are?

Simply put, you can type a large number like 22@q (since 2 and @ correspond to the same key) to make the macro execute as much as possible.

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

In the example above, when Vim reaches the last line (there is only one}), the replacement cannot be done because there is no., and Vim stops execution there instead of actually executing 22 times.

The editor

What if I want to make some other edits to the macro besides append?

Earlier we recorded the macro in a register that is the same as when we copy, delete, and so on. When we record a macro, Vim stores our recorded sequence of operations in a register as a piece of text.

To edit macros, the basic idea is to paste the text in the register, edit it, and then save it back to the register.

Take the macro you just recorded in register Q:

  • Put the contents of register Q with “qp”

  • Edit the copied text

  • Copy this line into register Q with “qyy”

  • Delete this line of text DD (because it is now useless)

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

  • Input: let @ a = ‘

  • Press < Ctrl-R > A to insert the text (the macro we just recorded) in register Q

  • Edit the copied text

  • Fill in the missing ‘and press Enter

You probably don’t need macros

Going back to the goal, this example is just to demonstrate macro use, but if you look into Vim’s visual mode you will see that this goal does not require macros, recording, execution, etc.

In Vim, press < Ctrl-v > to enter “block viewable mode”, which allows you to edit multiple lines of text at the same time.

  • F. Find the character to be modified.

  • < Ctrl-v > JJJJ select the row you want to modify

  • R – Completes the substitution

As you can see, each selected row is replaced with only the first., rather than the entire row being selected as in normal visual mode.