Explore VIM advanced operation skills
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." |
First, we open VIM and enter a piece of text for today’s demonstration:
this is a test2. 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
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 splicingJust 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 test4. 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
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 operationsNext 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
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
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 recordingNext, 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
解说:虽然我们指定播放 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!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!
解说:点命令的作用是,重复最近一次所做的编辑操作。由于在第一行里做的操作是行尾添加并插入新行,因此在第三行(原先的第二行)重复这个动作的时候,也会在行尾添加同样的字符。点命令功能不如宏强大,但它使用起来比宏简便,因此也有着广泛的用途。
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!

The main tasks of Linux system administrators include system monitoring and performance tuning, user management, software package management, security management and backup, troubleshooting and resolution, performance optimization and best practices. 1. Use top, htop and other tools to monitor system performance and tune it. 2. Manage user accounts and permissions through useradd commands and other commands. 3. Use apt and yum to manage software packages to ensure system updates and security. 4. Configure a firewall, monitor logs, and perform data backup to ensure system security. 5. Troubleshoot and resolve through log analysis and tool use. 6. Optimize kernel parameters and application configuration, and follow best practices to improve system performance and stability.

Learning Linux is not difficult. 1.Linux is an open source operating system based on Unix and is widely used in servers, embedded systems and personal computers. 2. Understanding file system and permission management is the key. The file system is hierarchical, and permissions include reading, writing and execution. 3. Package management systems such as apt and dnf make software management convenient. 4. Process management is implemented through ps and top commands. 5. Start learning from basic commands such as mkdir, cd, touch and nano, and then try advanced usage such as shell scripts and text processing. 6. Common errors such as permission problems can be solved through sudo and chmod. 7. Performance optimization suggestions include using htop to monitor resources, cleaning unnecessary files, and using sy

The average annual salary of Linux administrators is $75,000 to $95,000 in the United States and €40,000 to €60,000 in Europe. To increase salary, you can: 1. Continuously learn new technologies, such as cloud computing and container technology; 2. Accumulate project experience and establish Portfolio; 3. Establish a professional network and expand your network.

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

The Internet does not rely on a single operating system, but Linux plays an important role in it. Linux is widely used in servers and network devices and is popular for its stability, security and scalability.

The core of the Linux operating system is its command line interface, which can perform various operations through the command line. 1. File and directory operations use ls, cd, mkdir, rm and other commands to manage files and directories. 2. User and permission management ensures system security and resource allocation through useradd, passwd, chmod and other commands. 3. Process management uses ps, kill and other commands to monitor and control system processes. 4. Network operations include ping, ifconfig, ssh and other commands to configure and manage network connections. 5. System monitoring and maintenance use commands such as top, df, du to understand the system's operating status and resource usage.

Introduction Linux is a powerful operating system favored by developers, system administrators, and power users due to its flexibility and efficiency. However, frequently using long and complex commands can be tedious and er

Linux is suitable for servers, development environments, and embedded systems. 1. As a server operating system, Linux is stable and efficient, and is often used to deploy high-concurrency applications. 2. As a development environment, Linux provides efficient command line tools and package management systems to improve development efficiency. 3. In embedded systems, Linux is lightweight and customizable, suitable for environments with limited resources.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.