您的位置:首页 > 其它

[Practical.Vim(2012.9)].Drew.Neil.Tip50 学习摘要

2015-04-13 23:27 363 查看

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上,但这个字符最后从删除操作中排除掉了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  search vim