Merge pull request #5220 from xiaow6/master

translated 20170210 How to perform search operations in Vim.md
This commit is contained in:
Chang Liu 2017-03-03 21:16:26 +08:00 committed by GitHub
commit 2c481a329e
3 changed files with 152 additions and 163 deletions

View File

@ -1,162 +0,0 @@
translating by xiaow6
How to perform search operations in Vim
============================================================
### On this page
1. [Customize your search][5]
1. [1\. Search highlighting][1]
2. [2\. Making search case-insensitive][2]
3. [3\. Smartcase search][3]
4. [4\. Incremental search][4]
2. [Some other cool Vim search tips/tricks][6]
3. [Conclusion][7]
While we've already [covered][8] 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:
[
![Search for words in vim](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-basic-search.png)
][9]
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.
[
![Move to next search hit](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-end.png)
][10]
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:
[
![search backwards](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-back.png)
][11]
### 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:
[
![Search Highlighting in VIM](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-highlight-search.png)
][12]
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
```
[
![set hlsearch](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-set-hlsearch.png)
][13]
### 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:
[
![search case-insensitive](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-case.png)
][14]
### 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 'terminal'), 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:
[
![grouped search results](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-results-list.png)
][15]
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][16].
### 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.
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/
作者:[Himanshu Arora][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/
[1]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-search-highlighting
[2]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-making-searchnbspcaseinsensitive
[3]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-smartcase-search
[4]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-incremental-search
[5]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#customize-your-search
[6]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#some-other-cool-vim-search-tipstricks
[7]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#conclusion
[8]:https://www.howtoforge.com/tutorial/vim-editor-modes-explained/
[9]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-basic-search.png
[10]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-end.png
[11]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-back.png
[12]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-highlight-search.png
[13]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-set-hlsearch.png
[14]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-case.png
[15]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-results-list.png
[16]:http://vim.wikia.com/wiki/Searching

View File

@ -1,3 +1,4 @@
translating by xiaow6
Using Scripting Languages in IoT: Challenges and Approaches
============================================================
@ -65,6 +66,6 @@ via: https://www.linux.com/news/event/elcna/2017/2/using-scripting-languages-iot
[2]:https://www.linux.com/licenses/category/creative-commons-zero
[3]:https://www.linux.com/files/images/paul-sokolovsky-2014-09-21jpg
[4]:https://www.linux.com/files/images/scripting-languages-iotjpg
[5]:http://events.linuxfoundation.org/events/embedded-linux-conference/program/schedule?utm_source=linux&utm_campaign=elc17&utm_medium=blog&utm_content=video-blog
[5]:http://events.linuxfoundation.org/events/embedded-linux-conference/program/schedule?utm_source=linux&utm_campaign=elc17&utm_medium=blog&utm_content=video-blog
[6]:http://events.linuxfoundation.org/events/embedded-linux-conference
[7]:https://events.linuxfoundation.org/events/openiot-summit/program/schedule

View File

@ -0,0 +1,150 @@
如何在Vim中实现搜索
================================
### 本页中
1. [自定义你的搜索][5]
[1\.1\.高亮搜索结果][1]
[1\.2\.使搜索不区分大小写][2]
[1\.3\.智能大小写搜索][3]
[1\.4\.递进搜索][4]
2. [其他很酷的在Vim中搜索的小技巧][6]
3. [结语][7]
尽管目前我们已经[涉及][8] Vim 的多种特性,但此编辑器的特征集如此庞大,不管我们学习多少,似乎仍然远远不足。承接我们的 Vim 教程系列,本文我们将讨论 Vim 提供的多种搜索技术。
不过在此之前,请注意文中涉及到的所有的例子、命令、指令均是在 Ubuntu 14.04Vim 7.4 下测试的。
### Vim 中的基础搜索操作
当你在 Vim 中打开一个文件并且想要搜索一个特定的单词或模板,第一步你必须要先从插入模式中退出(如果你正处于插入模式中)。之后输入 **/**’(不带引号)并紧接着输入你要搜索的单词或模板。
例如,如果你想要搜索的单词是 linux',下图显示的就是在 Vim 窗口底部的搜索命令:
[![Search for words in vim](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-basic-search.png) ][9]
敲击回车键之后,你会看到 Vim 会将光标停留在从光标在插入模式中的位置开始,找到的包含此单词的第一行。如果你刚刚打开一个文件并且立即开始了搜索操作,搜索将从文件的首行开始。
如果想要移动到下一处包含被搜索单词位置,按 **n**' 键。当你遍历完所有被搜索模板所在之处,继续按 **n**' 键 Vim将重复搜索操作光标将回到第一次搜索结果出现位置。
[ ![Move to next search hit](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-end.png) ][10]
在遍历搜索结果时,如果你想要回到上一匹配处,按 '**N**' (shift + n)。同时,值得注意的是不管在什么时候,你都可以输入 '**ggn**' 来跳转到第一个匹配处,或者 **GN**' 来跳转到最后一处。
当你恰好在文件的底部,而且想要逆向搜索的情况下,使用 **** 代替 **/** 来开始搜索。下图是一个例子:
[![search backwards](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-back.png)][11]
### 自定义你的搜索
### 1\. 高亮搜索结果
尽管通过 n' 或 'N' 从被搜索单词 / 模板的匹配处跳转到另一处很简单,但是如果匹配处能够高亮就更加人性化了。例如,请看下附截图:
[![Search Highlighting in VIM](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-highlight-search.png) ][12]
这可以通过设置 hlsearch 变量来实现,例如在普通/命令行模式中执行下述命令:
```
:set hlsearch
```
[![set hlsearch](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-set-hlsearch.png) ][13]
### 2\. 使搜索不区分大小写
在 Vim 中进行搜索默认是区分大小写的。这就意味着如果我要搜索 linux',那么 Linux 是不会匹配的。然而,如果这不是你想要的搜索方式,你可以使用如下命令来使搜索变得不区分大小写:
```
:set ignorecase
```
所以当我设置 ignorecase 变量后再使用前边提到的命令,搜索 'linux',那么 Linux 所在处也会被高亮。
[![search case-insensitive](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-search-case.png) ][14]
### 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](https://www.howtoforge.com/images/perform-search-operations-in-vim/vim-results-list.png) ][15]
接下来你可能已经得知Vim 默认是环形搜索的,意味着在到达文件结尾处(或者被搜索单词的最后一处匹配)时,如果继续按 “搜索下一个”会将光标再次带回第一处匹配处。如果你希望禁止环形搜索,可以使用如下命令:
```
:set nowrapscan
```
再次开启环形搜索,使用如下命令即可:
```
:set wrapscan
```
最后,假设你想要对文件中已经存在的单词做一点小小的修改,然后对修改后的单词执行搜索操作,一种方法是输入 **/** 与要搜索的单词。但是如果这个单词又长又复杂,那么可能需要一点时间来输入它。
一个简单的办法是将光标移到你想要略微修改的单词下,按 /' 之后再按 Ctrl - r 最后按 Ctrl - w。这个在光标下的单词不仅仅会被拷贝也会被复制到 /' 后,允许你对它进行修改并且继续进行搜索操作。
如果想要获得更多小技巧(包括如何使用鼠标来使在 Vim 中的操作变得简单),请前往 [Vim 官方文档][16]。
### 结语
当然,没有人希望你死记硬背这里提到的所有小技巧。你应该做的是,从一个你认为对你最有益的技巧开始不断练习。当它成为一种习惯并且嵌入你的记忆后,重新来这儿找找你应该开始学习的下一个技巧。
你知道其他像这样的技巧吗?并且希望能够和大家一起分享?那就在下边留言吧!
--------------------------------------------------------------------------------
via: https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/
作者:[Himanshu Arora][a]
译者:[xiaow6](https://github.com/xiaow6)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/
[1]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-search-highlighting
[2]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-making-searchnbspcaseinsensitive
[3]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-smartcase-search
[4]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#-incremental-search
[5]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#customize-your-search
[6]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#some-other-cool-vim-search-tipstricks
[7]:https://www.howtoforge.com/tutorial/perform-search-operations-in-vim/#conclusion
[8]:https://www.howtoforge.com/tutorial/vim-editor-modes-explained/
[9]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-basic-search.png
[10]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-end.png
[11]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-back.png
[12]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-highlight-search.png
[13]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-set-hlsearch.png
[14]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-search-case.png
[15]:https://www.howtoforge.com/images/perform-search-operations-in-vim/big/vim-results-list.png
[16]:http://vim.wikia.com/wiki/Searching