超酷的 Vim 搜索技巧
Vim 搜索
尽管目前我们已经[涉及](/article-8224-1.html) Vim 的多种特性，但此编辑器的特性集如此庞大，不管我们学习多少，似乎仍然远远不足。承接我们的 Vim 教程系列，本文我们将讨论 Vim 提供的多种搜索技术。


不过在此之前，请注意文中涉及到的所有的例子、命令、指令均是在 Ubuntu 14.04，Vim 7.4 下测试的。


![](/data/attachment/album/201703/10/092612n343i4483idztaxb.jpg)


### Vim 中的基础搜索操作


当你在 Vim 中打开一个文件并且想要搜索一个特定的单词或模板，第一步你必须要先按下 `Esc` 键从插入模式中退出（如果你正处于插入模式中）。之后输入 `/` 并紧接着输入你要搜索的单词或搜索模式。


例如，如果你想要搜索的单词是 `linux`，下图显示的就是在 Vim 窗口底部的搜索命令：


![Search for words in vim](/data/attachment/album/201703/10/092617dhb78b595zft58y3.png)


敲击回车键之后，你会看到 Vim 会将光标停留在从光标在插入模式中的位置开始，找到的包含此单词的第一行。如果你刚刚打开一个文件并且立即开始了搜索操作，搜索将从文件的首行开始。


如果想要移动到下一处包含被搜索单词位置，按 `n` 键。当你遍历完所有被搜索模板所在之处，继续按 `n` 键 Vim 将重复搜索操作，光标将回到第一次搜索结果出现位置。


![Move to next search hit](/data/attachment/album/201703/10/092618xrk2988qi6i4qs63.png)


在遍历搜索结果时，如果你想要回到上一匹配处，按 `N` (即 `shift` + `n`)。同时，值得注意的是不管在什么时候，你都可以输入 `ggn` 来跳转到第一个匹配处，或者 `GN` 来跳转到最后一处。


当你恰好在文件的底部，而且想要逆向搜索的情况下，使用 `?` 代替 `/` 来开始搜索。下图是一个例子：


![search backwards](/data/attachment/album/201703/10/092618z9e7b2ak00ges90d.png)


### 自定义你的搜索


#### 1、 高亮搜索结果


尽管通过 `n` 或 `N` 从被搜索单词或模式的匹配处跳转到另一处很简单，但是如果匹配处能够高亮就更加人性化了。例如，请看下附截图：


![Search Highlighting in VIM](/data/attachment/album/201703/10/092619yskkkxk8535xtjbk.png)


这可以通过设置 `hlsearch` 变量来实现，例如在普通/命令行模式中执行下述命令：



```
:set hlsearch

```

![set hlsearch](/data/attachment/album/201703/10/092619lsg8p8hh0tx787qp.png)


#### 2、使搜索不区分大小写


在 Vim 中进行搜索默认是区分大小写的。这就意味着如果我要搜索 `linux`，那么 `Linux` 是不会匹配的。然而，如果这不是你想要的搜索方式，你可以使用如下命令来使搜索变得不区分大小写：



```
:set ignorecase

```

所以当我设置 `ignorecase` 变量后再使用前边提到的命令，搜索 `linux`，那么 `Linux` 所在处也会被高亮。


![search case-insensitive](/data/attachment/album/201703/10/092619w777cwhntt3h3q0z.png)


#### 3、智能大小写搜索


Vim 提供了一个功能，只有当要搜索的单词 / 模板包含大写字母时，编辑器才会区分大小写。要想实现这种功能，必须先设置 `ignorecase`，再接着设置 `smartcase` 变量。



```
:set ignorecase
:set smartcase

```

例如，如果一个文件中既包含 `LINUX` 也包含 `linux`，在开启智能大小写搜索功能时，如果使用 `/LINUX` 进行搜索，只有单词 `LINUX` 处会被搜到。反之，如果搜索 `/linux`，那么不论大小写的搜索结果都会被匹配。


#### 4、递进搜索


就如谷歌一样，随着你输入查询字串（字串随你每输入一个字符不断更新）显示不同的搜索结果，Vim 也同样提供了递进搜索。要想使用这种特性，你必须在搜索前执行下述命令：



```
:set incsearch

```

### 一些很酷的在 Vim 中搜索的小技巧


你可能会发现还有一些其他的与搜索相关的小技巧很有用。


开始吧！如果你想要搜索一个文件中的一个单词，但是又不想输入它，你只需要将你的光标移到这个单词下然后按 `*` （或者 `shift` + `8`）。如果你想要启动一次部分搜索（例如：同时搜索 `in` 和 `terminal`），那你需要将光标移到到单词（在本例中， `in`）下，然后通过在键盘上按 `g*` （按一次 `g` 然后不断按 `*` ）。


注意：如果你想要逆向搜索，按 `#` 或者 `g#` 。


这里有个视频，你可以看看：







下一个，只要你想要，你可以获得所有被搜索单词或模式匹配处所在的行和行号的一个列表。这可以在你开始搜索后通过按 `[I` 来实现。如下图是一个列表结果如何在 Vim 窗口底部被分组和显示的例子：


![grouped search results](/data/attachment/album/201703/10/092620agw7cnxm7wclgnrw.png)


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



```
:set nowrapscan

```

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



```
:set wrapscan

```

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


一个简单的办法是将光标移到你想要略微修改的单词下，按 `/` 之后再按 `Ctrl` + `r` 最后按 `Ctrl` + `w`。这个在光标下的单词不仅仅会被拷贝，也会被复制到 `/` 后，允许你对它进行修改并且继续进行搜索操作。


如果想要获得更多小技巧（包括如何使用鼠标来使在 Vim 中的操作变得简单），请前往 [Vim 官方文档](http://vim.wikia.com/wiki/Searching)。


### 结语


当然，没有人希望你死记硬背这里提到的所有小技巧。你应该做的是，从一个你认为对你最有益的技巧开始不断练习。当它成为一种习惯并且嵌入你的记忆后，重新来这儿找找你应该开始学习的下一个技巧。


你知道其他像这样的技巧吗？并且希望能够和大家一起分享？那就在下边留言吧！




---


via: <https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/>


作者：[Himanshu Arora](https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/) 译者：[xiaow6](https://github.com/xiaow6) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# How to perform search operations in Vim

While we've already [covered](https://www.howtoforge.com/tutorial/vim-editor-modes-explained/) several features of Vim until now, the editor's feature-set is so vast that no matter how much you learn, it doesn't seem to be enough. So continuing with our Vim tutorial series, in this write-up, we will discuss the various search techniques that the editor offers.

`But before we do that, please note that all the examples, commands, and instructions mentioned in this tutorial have been tested on Ubuntu 14.04, and the Vim version we've used is 7.4.`

## Basic search operations in Vim

If you have opened a file in the Vim editor, and want to search a particular word or pattern, the first step that you have to do is to come out of the Insert mode (if you that mode is currently active). Once that is done, type '**/**' (without quotes) followed by the word/pattern that you want to search.

For example, if the word you want to search is 'linux', here's how it will appear at the bottom of your Vim window:

After this, just hit the Enter key and you'll see that Vim will place the cursor on the first line (containing the word) that it encounters beginning from the line where the cursor was when you were in Insert mode. If you've just opened a file and began searching then the search operation will start from the very first line of the file.

To move on to the next line containing the searched word, press the '**n**' key. When you've traversed through all the lines containing the searched pattern, pressing the '**n**' key again will make the editor to repeat the search, and you'll be back to the first searched occurrence again.

While traversing the searched occurrences, if you want to go back to the previous occurrence, press '**N**' (shift+n). Also, it's worth mentioning that at any point in time, you can type '**ggn**' to jump to the first match, or '**GN**' to jump to the last.

In case you are at the bottom of a file, and want to search backwards, then instead of initiating the search with ** /**, use

**. Here's an example:**

*?*## Customize your search

### 1. Search highlighting

While jumping from one occurrence of the searched word/pattern to another is easy using 'n' or 'N,' things become more user-friendly if the searched occurrences get highlighted. For example, see the screenshot below:

This can be made possible by setting the 'hlsearch' variable, something which you can do by writing the following in the normal/command mode:

:set hlsearch

### 2. Making search case-insensitive

By default, the search you do in Vim is case-sensitive. This means that if I am searching for 'linux', then 'Linux' won't get matched. However, if that's not what you are looking for, then you can make the search case-insensitive using the following command:

:set ignorecase

So after I set the 'ignorecase' variable using the aforementioned command, and searched for 'linux', the occurrences of 'LINUX' were also highlighted:

### 3. Smartcase search

Vim also offers you a feature using which you can ask the editor to be case-sensitive only when the searched word/pattern contains an uppercase character. For this you need to first set the 'ignorecase' variable and then set the 'smartcase' variable.

:set ignorecase

:set smartcase

For example, if a file contains both 'LINUX' and 'linux,' and smartcase is on, then only occurrences of the word LINUX will be searched if you search using '/LINUX'. However, if the search is '/linux', then all the occurrences will get matched irrespective of whether they are in caps or not.

### 4. Incremental search

Just like, for example, Google, which shows search results as you type your query (updating them with each alphabet you type), Vim also provides incremental search. To access the feature, you'll have to execute the following command before you start searching:

:set incsearch

## Some other cool Vim search tips/tricks

There are several other search-related tips tricks that you may find useful.

To start off, if you want to search for a word that's there in the file, but you don't want to type it, you can just bring your cursor below it and press ***** (or **shift+8**). And if you want to launch a partial search (for example: search both '*in'* and 'term*in*al'), then you can bring the cursor under the word (in our example, *in*) and search by pressing **g*** (press 'g' once and then keep pressing *) on the keyboard.

Note: Press **#** or **g#** in case you want to search backwards.

Next up, if you want, you can get a list of all occurrences of the searched word/pattern along with the respective lines and line numbers at one place. This can be done by type **[I** after you've initiated the search. Following is an example of how the results are grouped and displayed at the bottom of Vim window:

Moving on, as you might already know, the Vim search wraps by default, meaning after reaching the end of the file (or to the last occurrence of the searched word), pressing "search next" brings the cursor to the first occurrence again. If you want, you can disable this search wrapping by running the following command:

:set nowrapscan

To enable wrap scan again, use the following command:

:set wrapscan

Finally, suppose you want to make a slight change to an already existing word in the file, and then perform the search operation, then one way is to type **/** followed by that word. But if the word in long or complicated, then it may take time to type it.

An easy way out is to bring the cursor under the word you want to slightly edit, then press '/' and then press Ctrl-r followed by Ctrl-w. The word under the cursor will not only get copied, it will be pasted after '/' as well, allowing you to easily edit it and go ahead with the search operation.

For more tricks (including how you can use your mouse to make things easier in Vim), head to the [official Vim documentation](http://vim.wikia.com/wiki/Searching).

## Conclusion

Of course, nobody expects you to mug up all the tips/tricks mentioned here. What you can do is, start with the one you think will be the most beneficial to you, and practice it regularly. Once it gets embedded in your memory and becomes a habit, come here again, and see which one you should learn next.

Do you know any more such tricks? Want to share it with everyone in the HTF community? Then leave it as a comment below.