Home > System Tutorial > LINUX > Explore VIM advanced operation skills

Explore VIM advanced operation skills

WBOY
Release: 2024-01-05 10:52:02
forward
954 people have browsed it
Introduction This article collects some simple VIM operations that either cannot be completed by other ordinary text editors or are completed very slowly. Through the introduction of this article, beginners can strengthen their determination and confidence in learning VIM. If you have any good simple techniques that are easy to demonstrate, please leave a message. In addition, unless otherwise stated, the keys mentioned in this article are case-sensitive. For example, when the text says "press G," the key you press should be "Shift G."

Explore VIM advanced operation skills

1. Preparation

First, we open VIM and enter a piece of text for today’s demonstration:

this is a test
Copy after login
Copy after login
2. Find and replace

Press ESC several times to enter Normal mode and enter the following command: :%s/ /\r/g/ . The effect obtained after pressing Enter is as follows:

this
is
a
test
Copy after login

Explanation: The function of this command is to replace all spaces in the article with carriage returns. Almost all editors support find and replace, but not all editors support replacing spaces with carriage returns, so this function is relatively cumbersome to implement in many other editors.

3. Line splicing

Just now we broke a line of text into 4 lines, so how do we splice them back together? Of course, we can use the search and replace method mentioned above to splice lines by replacing carriage returns with spaces. However, here we use another method.

Press ESC several times to enter Normal mode, and then enter this command: ggVG. gg means to jump to the beginning of the text, V means to enter line selection mode, and G means to select to the end of the article. With these 3 commands, 4 keystrokes in total, we selected the entire article.

Then, press the colon: enter the command mode, the status bar appears: :', enter j after it, and then press Enter, you can see the entire article The article was spliced ​​together again, and the entire operation including Enter only required 7 key presses:

this is a test
Copy after login
Copy after login
4. Copy, paste and repeat actions

Press ESC several times to confirm that you are currently in Normal mode, and then press yy to copy the current line to the default register (equivalent to the clipboard). Then press 12p, VIM will perform the paste action 12 times, and 13 lines of characters like this will appear on the screen:

this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
this is a test
Copy after login

Explanation: In VIM, copy and paste operations are very fast. In addition, most commands in VIM can be repeated several times by adding a number before the command.

5. Column operations

Next we change the first letter at the beginning of each line to uppercase.

Press ESC several times to confirm that you are in Normal mode, then press gg to jump to the first line, and press Ctrl v to enter column selection mode (if you press Ctrl v and fail to enter column selection mode (see here), then press G to jump to the last line of the article. At this point you should see that the first column of text is selected, and only the first column is selected. Press the U key and you will see that the first letter of each line becomes uppercase. Tip: After selecting the text, press u to change the text to lower case. After selecting the text, press ~ to flip the original case.

This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
This is a test
Copy after login

Then, we add an asterisk in front of each line. Press gg to jump to the first line, press Ctrl v to enter column selection mode, then press G to select the first column of the full text, then press I to enter column insertion state, enter asterisk *, and then press Press ESC and you will see that an asterisk appears before all lines:

*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
*This is a test
Copy after login

Explanation: For those who write programs, commenting out a piece of code in batches is a very common operation. This can be easily done using column insertion. In addition, you can press x after selecting a column to delete the selected blocks, and you can uncomment in batches.

6. Macro recording

Next, we want to change the even-numbered lines of text to: This is another test. Since all even-numbered rows need to perform the same operation, we can quickly complete the work by recording this operation and then playing it repeatedly several times.

First, press ESC several times to confirm that you are in Normal mode, then press gg to jump to the first line and prepare to start the operation. We first press the q key, and then press another letter to record this macro into the register corresponding to that letter. For example, if we use the m register here, press qm. At this time, the word "recording" appears in the VIM status bar, indicating that it has entered the recording state.

然后,我们把第二行的 a 修改为 another。首先按 j 进入第二行,然后按 $ 跳到行末,再按两下 b 往前跳两个单词,此时光标停在字母 a 上。然后我们按下 caw键删除 a 并进入插入状态,然后输入 another ,按 ESC 回到 Normal 状态,按 j 进入下一行,整个操作步骤就完成了。最后,我们再按一下 q,结束该宏的录制。

接下来我们播放这个宏,完成整个操作步骤。在键盘上输入 1000@m,表示将 m 寄存器里的宏播放 1000 次,马上可以看到,文章中所有偶数行的 a 都变成了 another。

*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
*This is another test
*This is a test
Copy after login

解说:虽然我们指定播放 1000 次,但事实上,执行到第 6 次的时候,光标挪到了屏幕最下方,于是执行过程就自动停止了。因此,在批量操作的时候,我们可以指定足够大的数字,而不用担心出现问题。

另外,修改 a 的时候,我们跳到行末后再使用 b 命令以单词为单位跳转,而没使用 h 一个字母一个字母往回挪,我们使用caw 修改整个单词,而不使用 s 命令删除单个字母并进入 Insert模式。这些细节可以保证录制得到的宏更具有一般性。

7. 行尾块操作

注:本章由 Jason Han 网友贡献,感谢他来信指出滇狐原先对于行尾块操作理解的错误。

下面,我们要在每行的尾部都添加一个感叹号。之前我们在每行头部添加一个星号的时候,用的是 Ctrl-V 列操作。现在要在行尾添加,能不能继续用列操作呢?直观上似乎是不行的,每行的长度不一样,行尾位置参差不齐,如何使用列模式往行尾添加东西呢?

事实上,Vim 提供了一种特殊的列模式,叫做行尾块模式,也就是说,我们是可以通过 Ctrl-V模式来选中长度不同的行的行尾,然后对行尾作统一操作的,操作步骤如下:

按下 gg 跳到第一行,按 Ctrl-V 进入列选择模式,再按 G,选中全文的第一列,然后按下 $,进入行尾块模式,按下 A,进入块插入状态,输入星号 !,再按下 ESC,你会看到,所有行尾部都出现了一个感叹号:

*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
Copy after login
Copy after login
8. 点命令

接下来,我们在每行的末尾加上一个小于号 。

由于我们需要在每行后面添加新行,因此我们无法使用块选择方式批量添加小于大于号。使用宏录制的方式是可以做到这点的,但操作稍嫌繁琐了一些。使用点命令,可以非常方便地做到这一点。

先按几下 ESC 确认当前出于 Normal 模式,然后使用 gg 跳到第一行,按 A 进行行尾插入,输入 ,最后 ESC 回到 Normal 状态,第一行修改就完成了。

然后,我们按 j 进入下一行,也就是第三行,再按 .,可以看到,第三行尾部也出现了小于号,并且自动添加了第四行的大于号。反复按j.j.j. ,直到每一行都完成了这个编辑动作为止。

*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
*This is another test!
*This is a test!
Copy after login
Copy after login

解说:点命令的作用是,重复最近一次所做的编辑操作。由于在第一行里做的操作是行尾添加并插入新行,因此在第三行(原先的第二行)重复这个动作的时候,也会在行尾添加同样的字符。点命令功能不如宏强大,但它使用起来比宏简便,因此也有着广泛的用途。

The above is the detailed content of Explore VIM advanced operation skills. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
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