📜 ⬆️ ⬇️

Macros in Vim are just

Macros in Vim


Very strange, on one side the topic is rather banal, but on the other, neither here nor on the Internet can be seen a good understanding of such an important topic, which can sometimes greatly simplify the task of text editing. The writing of macros in Emacs on Avdi Grimm's blog pushed me toward writing, where no one at one time could clearly explain that Vim has what the author of the post expects from a decent text editor. We will correct this, first of all in ourselves, and then we will go and say these from the Internet that they are wrong.

What text editing tasks can help solve macros? For example, this: in this very line that you read, take all commas to change to 0. You can remember regular expressions, but not all love them, and certainly not all remember how to insert a line break with them, but if you need to replace some characters that are used in regular expressions as service ones, you are already distracted from your main task and instead of editing the text, you are writing a regular expression for search and replace, trying and making mistakes.
Macros allow you to do this faster, with less distraction to remembering and writing something. Remembering how macros work is very easy, and it will go into your daily arsenal along with jumping through the text.

In this topic, we will learn how to use macros with this very task.
')
Let's first remember how to do it manually.
f,r0
Then you can press sequentially ; to re-search and . to repeat the previous action. But at some point it may not be so easy, for example, you need to add a couple of signs between the case. The point no longer works and you will need to press r0 again. Or these commas will be quite a lot, and click alternately ; and will have quite a few times. At this point, something already tells you that such mechanical work can be automated.

The basics
Repeat and recursive call
Macro editing
Eternal Macros
Any

The basics


Vim uses registers to record and store macros. The ones in which you can copy the text and the ones on which you can put marks. Registers are denoted by Latin letters, case-insensitive (a and A are the same register), numbers, and even special characters, that is, there will be enough of them for all.

Is important . Not something magical is stored in registers; simple text is stored in registers.

To write a macro to register a use the following command:
qaf,r0q
The first qu q starts recording a macro, and so on before pressing q to confirm the record.
Now, to run the macro again, we need to press @a , and the macro stored in register a will be launched for execution. We can traditionally write 100@a for Vim, and the macro will be executed 100 times, replacing 100 or less (if there are less of them in the string) commas by 0 .

Repeat and recursive call


Sometimes we don’t know how many commas we have in the string, so after running 100@a we have to manually check if there is any more.
It can be quite simple to make the macro invoke itself. We write the same macro, which will need to be called only once, and then it will call itself:
qaf,r0@aq
In this case, his record has not yet been produced, but the launch has already been specified. It is important that nothing is stored in this register at the time of recording. There are two ways to do this. The first is to clean the contents of the register before recording with another three clicks, qaq , that is, writing an empty sequence. The second is to make a call to the last macro called using @@ .

Is important . The execution of a chain or series of execution of macros is suspended at the very moment when one of the fFtT commands, that is, searching for a character in a string, fails. That is, for example, we have no more in the comma string. Naturally, even if you indicated that the macro needed to be executed 100 times, and there were only two commas, the macro will run twice, and the third will stop searching and will not continue any more. Similarly, with a recursive macro.

Conveniently, when recording a macro, its first execution takes place, that is, we immediately see if it was recorded correctly.

Macro editing


If something went wrong, and you want to see what we wrote down, you can use the standard Vim function, and paste the contents of register a directly into the text: "ap .
Right in the editor you will see the text, which is a sequence of commands:
 af,r0@a 

Suppose you decide that you need to change the text not to 0 , but to 1 , and you edit the text of the macro:
 af,r1@a 

and copy it to register a with 0"ay$ .

Eternal Macros


It is worth noting that the registers are permanent, if you exit Vim and then log in again, the contents of the register will remain untouchable. It is stored by default in the file ~/.viminfo .

Is important . Register contents are easily accidentally overwritten, and if you want some macros to exist in some registers all the time, it makes sense to add them to .vimrc , or better, to a separate macro file included in .vimrc :
  let @a='af,r1@a' 


Any


Macros do not necessarily operate within a single line; try jumping over paragraphs in macros to change the case of the first word to the top one.

To avoid accidental transitions by pressing Q in Ex mode, it makes sense to assign Q to a call to the previous macro:
  nnoremap Q @@ 


Useful information:
:help registers
:help :registers
:help :recording

For further study:
Advanced Vim Macros

Source: https://habr.com/ru/post/230487/


All Articles