Search techniques for vim editor in Linux
The vim editor is one of the most advanced text editors in Unix systems. It supports almost all file types and is also called a programmer's editor. In this tutorial, we will learn how to perform search operations in the vim editor to quickly find the text we need.

But before that, please note that all examples, commands, and instructions mentioned in the article were tested under Ubuntu 14.04 and Vim 7.4.
Basic search operations in Vim
When you open a file in Vim and want to search for a specific word or template, the first step is to exit insert mode by pressing the Esc key (if you are in insert mode). Then type / followed by the word or search pattern you want to search for.
For example, if the word you want to search for is linux, the following figure shows the search command at the bottom of the Vim window:
After hitting the Enter key, you will see that Vim will place the cursor on the first line containing this word found starting from the cursor position in insert mode. If you have just opened a file and started searching immediately, the search will start from the first line of the file.
If you want to move to the next position containing the searched word, press the n key. When you have traversed all the locations of the searched templates, continue to press the n key. Vim will repeat the search operation, and the cursor will return to the position where the first search result appeared.

When traversing the search results, if you want to return to the previous match, press N (i.e. shift n). At the same time, it is worth noting that at any time, you can enter ggn to jump to The first matching point, or GN to jump to the last one.
When you happen to be at the bottom of the file and want to search in reverse, use ? instead of / to start the search. The picture below is an example:
Customize your search
1. Highlight search results
Although it is simple to jump from the matching point of the searched word or pattern to another place through n or N, it would be more user-friendly if the matching point could be highlighted. For example, please see the attached screenshot:

This can be achieved by setting the hlsearch variable, for example, executing the following command in normal/command line mode:
:set hlsearch
2. Make the search case-insensitive
Searching in Vim is case-sensitive by default. This means that if I search for linux, Linux will not match. However, if this is not how you want to search, you can make the search case-insensitive using the following command:
:set ignorecase
So when I set the ignorecase variable and then use the command mentioned earlier to search for linux, the location of linux will also be highlighted.

3. Smart case search
Vim provides a feature where the editor will be case-sensitive only if the word/template to be searched for contains uppercase letters. To implement this functionality, you must first set ignorecase and then set the smartcase variable.
:set ignorecase :set smartcase
For example, if a file contains both LINUX and linux, when the smart case search function is turned on, if you use /LINUX to search, only the word LINUX will be searched. On the other hand, if you search for linux, the search results will be matched regardless of case.
4. Progressive search
Just like Google, which displays different search results as you enter the query string (the string is constantly updated with each character you enter), Vim also provides progressive search. To use this feature, you must execute the following command before searching:
:set incsearch
Some cool tips for searching in Vim
There are a few other search-related tips you may find useful.
let's start! If you want to search for a word in a file but don't want to type it, just move your cursor under the word and press * (or shift 8). If you want to start a partial search (for example, search for both in and terminal), then you need to move the cursor under the word (in this case, in) and then press g* on the keyboard (press g once and then Keep pressing * ).
Note: If you want to search in reverse, press # or g#.
Here is a video, you can watch it:
Video Player
00:00
03:33
下一个,只要你想要,你可以获得所有被搜索单词或模式匹配处所在的行和行号的一个列表。这可以在你开始搜索后通过按 [I 来实现。如下图是一个列表结果如何在 Vim 窗口底部被分组和显示的例子:

接下来,你可能已经得知,Vim 默认是环形搜索的,意味着在到达文件结尾处(或者被搜索单词的最后一处匹配)时,如果继续按 “搜索下一个” 会将光标再次带回第一处匹配处。如果你希望禁止环形搜索,可以使用如下命令:
:set nowrapscan
再次开启环形搜索,使用如下命令即可:
:set wrapscan
最后,假设你想要对文件中已经存在的单词做一点小小的修改,然后对修改后的单词执行搜索操作,一种方法是输入 / 与要搜索的单词。但是如果这个单词又长又复杂,那么可能需要一点时间来输入它。
一个简单的办法是将光标移到你想要略微修改的单词下,按/ 之后再按Ctrl +r 最后按 Ctrl块 + w 。这个在光标下的单词不仅仅会被拷贝,也会被复制到/ 后,允许你对它进行修改并且继续进行搜索操作。
如果想要获得更多小技巧(包括如何使用鼠标来使在 Vim 中的操作变得简单),请前往 Vim 官方文档。
在本教程中,我们介绍了如何在vim编辑器中进行搜索和替换,包括使用斜杠和点方法,使用冒号和s命令,以及使用全局命令。我们还学习了如何设置大小写敏感或不敏感,以及如何搜索整个单词。vim编辑器提供了强大而灵活的搜索功能,可以帮助我们提高编辑效率。
The above is the detailed content of Search techniques for vim editor in Linux. 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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

Dreamweaver CS6
Visual web development tools