Home > Operation and Maintenance > Linux Operation and Maintenance > Use VIM macro to complete batch operations

Use VIM macro to complete batch operations

齐天大圣
Release: 2020-06-11 13:17:43
Original
3298 people have browsed it

Let’s first look at the following two scenarios, commenting multiple lines of code in batches and indenting multiple lines of code. These two scenarios are often encountered in our daily development. If you are using an IDE, to batch comment multiple lines of code, just select these lines and then use ctrl /. If you want to indent, just select the line and press the tab key. However, if you use the vim editor, these operations are not completed in this way. Today, I would like to introduce to you a powerful tool under vim - macros. Using it, you can accomplish many unexpected functions!

It is difficult to understand the meaning of macro literally. In fact, you can just understand it as batch processing. Think of macro as a collection of many commands. These sets of commands can then be executed multiple times.

Using macros

The use of macros is divided into recording and playback. vim uses the q key to start recording, and it is also the q key to end the macro.

Use q{register} to select the register that needs to be kept. The function of this register is to save the recorded command in it.

Use @{register} to play back the command set in the register. If you want to play the macro multiple times, you can use the number @{register}, such as 10@a.

Practical

So much for the introduction of macros, now let’s complete the requirements mentioned at the beginning. First, complete the function of batch commenting code. Here, we do not use macros to complete it, but use block selection and command line mode to complete this requirement. For the second requirement, we use macros to complete it.

The following is a piece of code:

Use VIM macro to complete batch operations

We choose to comment out the free method.

First we come to line 70, press 70G in normal mode to come to line 70. Press V to enter block selection mode, then move the cursor to select all lines you want to comment. Input: Enter command line mode, enter normal 0i//, and then press the enter key.

Use VIM macro to complete batch operations

Next, let’s use macros to complete another requirement—batch indentation.

Use VIM macro to complete batch operations

You can see that there are many empty spaces on the left. Now let’s eliminate these useless spaces. First come to the first line, press qa to start recording the macro; press 0w, come to the first non-space string, press i, and then backspace until all spaces are deleted; then press the q key End the recording. At this point, the macro is ready. The next step is to play the macro for the remaining lines. Enter block selection mode, select all remaining rows, and press :normal @a.

Use VIM macro to complete batch operations

The above is the detailed content of Use VIM macro to complete batch operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template