Home > System Tutorial > LINUX > body text

Search techniques for vim editor in Linux

PHPz
Release: 2024-02-14 22:42:03
forward
478 people have browsed it

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.

Search techniques for vim editor in Linux

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: Search techniques for vim editor in Linux

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.

Search techniques for vim editor in Linux

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: Search techniques for vim editor in Linux

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:

Search techniques for vim editor in Linux

This can be achieved by setting the hlsearch variable, for example, executing the following command in normal/command line mode:

:set hlsearch
Copy after login

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
Copy after login

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.

Search techniques for vim editor in Linux

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
Copy after login

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
Copy after login

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 窗口底部被分组和显示的例子:

Search techniques for vim editor in Linux

接下来,你可能已经得知,Vim 默认是环形搜索的,意味着在到达文件结尾处(或者被搜索单词的最后一处匹配)时,如果继续按 “搜索下一个” 会将光标再次带回第一处匹配处。如果你希望禁止环形搜索,可以使用如下命令:

:set nowrapscan
Copy after login

再次开启环形搜索,使用如下命令即可:

:set wrapscan
Copy after login

最后,假设你想要对文件中已经存在的单词做一点小小的修改,然后对修改后的单词执行搜索操作,一种方法是输入 / 与要搜索的单词。但是如果这个单词又长又复杂,那么可能需要一点时间来输入它。

一个简单的办法是将光标移到你想要略微修改的单词下,按/ 之后再按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!

source:lxlinux.net
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