Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of macro commands in Vim

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the use of macro commands in Vim, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Preface

There is a saying in Vim's design philosophy: "if you write a thing once, it is okay. However if you're writing it twice or more times, then you should find a better way to do it".

Vim's Macro is used to solve the problem of repetition. Macro has been covered in the Vim register article, and macro operations are stored in registers in the form of text.

Macros are a collection of commands that are widely used, including word editors in MS Office, excel editors and various text editors. VIM, one of the most powerful text editors in the Linux system, also supports macro operations. This section briefly describes the use of macros in VIM. First, take a look at the commands of the following macros

Q* start recording macros

Q stop recording macros

@ * execute macros

Repeat the last @ * command once

The above commands are used in the general mode (Normal) of VIM, where * represents a number and word character, and the regular expression rule is: [0-9a-zA-Z]

: help recording view macro help

For example, when you enter qq in general mode, you will see that recording appears in the status bar below vim, which means that you have started recording the macro to register Q, or you can use Q as the id of the macro. All commands carried out at this time will be recorded into the macro, press Q again in the normal mode to stop the recording of the macro, and then you can call the macro through @ Q.

Easy to use

Record macro, using one of the 26 letters Q + [amurz]

Q [aMuz]

All subsequent commands are recorded, and then press Q at the end.

When performing macro, add @ to the register, such as recording it in register a

10 years a

Execute the command recorded in the a register 10 times.

@ @

Carry out the last order again.

Edit macro

Suppose you already have a macro saved in a, and you can use the

: let @ aura'

Enter Ctrl + r + a to insert the content in a

Edit the content and exit with 'end Enter

View macro

The macro content is saved in a, and you can directly use: reg a to view the content.

Give an 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, for example, replace:% sUnix, for example. Command, such as the macro to be used here:

Int a = 1int b = 2int c = a+bprint aprint bprint c

If it is to use. It is easy to use a simple file like this, but if the file has 1000 lines, it is obvious. Orders are not feasible. With macro, you can record it first and then execute it on 1000 lines.

For example, you can use qaA;jq in normal mode

Qa starts recording and stores it in register a

An enters insert mode at the end of the line

; insert semicolon

Exit insert mode

J next line

Q exit recording

At this point, the operation of the current line is saved in the a register, added on the current line, and the cursor is moved to the next line.

After recording, you can use @ a

1000 years a

Execute macro 1000 times to add the following 1000 lines to the end

Increasing number

You can use macro to insert 1 to 100 numbers, one for each line:

123...100

First insert 1 on the first line, then position the cursor at "1" and enter normal mode

Enter a command

Qayypaq

Yyp copies a line and pastes it on a new line

A number + 1

Q end recording

Final execution

Thank you for reading this article carefully. I hope the article "what is the use of macro commands in Vim" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report