標籤:search vim
Search to Navigate
The character search commands ( f{char} , t{char} , and so on) are fast and lightweight, but they have limitations. They can only search for a single character at a time, and they can only operate within the current line.
字元搜尋命令f{char} , t{char}
等,非常快捷,但是這些命令一次只能搜尋一個字元,而且只能在當前行中操作。
Suppose that we want to position our cursor on the word “takes ” in this sample:
假設我們在下面文本中要定位詞語“takes”
我們可以用搜尋命令 /takes
Operate with a Search Motion
We ’re not limited to using the search command in Normal mode. We can use it from Visual and Operator-Pending modes just as well to do real work. For example, suppose that we wanted to delete the text “takes time but eventually” from this phrase:
搜尋命令的使用方法不僅僅局限於此。我們可以在Visual和Operator Pending模式中使用來做一些其他有用的工作。比如,假設我們從下面文本中想要刪除“takes time but eventually”
To begin with, we press v to switch to Visual mode. Then we can extend the selection by searching for the short “ge ” string, which puts the cursor where we want it in a single bound. Well, almost—we have an off-by-one error. The selection includes the “g” at the start of the word, but we don ’t want to delete that. We ’ll use h to back up one character. Then, having defined our selection, we ’ll delete it with the d command.
首先,我們輸入v
切換到Visual模式,然後通過搜尋“ge”字串拓展選擇範圍,游標停留在了gets的第一個字元g上。但是有一個問題,選擇範圍的最後一個字元包含了字元g,我們並不要刪除它,所以再用命令h退後一個字元。最後,再使用d
命令刪除這個範圍內的字元。
Here ’s an even quicker way of doing the same thing:
下面介紹的是一個更加簡單的方法
Here, we use the /ge
search motion to tell the d{motion}
command what to delete. The search command is an exclusive motion. That means that even though our cursor ends up on the “g” at the start of the word “gets, ” that character is excluded from the delete operation
這裡,首先將游標定位在 takes上,然後利用/ge
搜尋命令告訴d{motion}
命令刪除到哪裡。這個搜尋操作是排除運動,意味著即使最後游標停留在gets的第一個字元g上,但這個字元最後從刪除操作中排除掉了。
[Practical.Vim(2012.9)].Drew.Neil.Tip50 學習摘要