搜索时,vi替换为空

时间:2016-07-11 18:40:59

标签: vim cygwin vi

在vi(来自cygwin),当我搜索时:

:%s/something

它只是替换了像

这样的空字符串
:%s/something//  .

我已经谷歌搜索了一段时间,但没有真正提到这一点。我应该添加到.vimrc或.exrc以使其工作吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

在vi和vim中,当您搜索模式时,只需键入/即可再次搜索。可以理解,当没有指定用于搜索的模式时,必须使用先前的模式。 (但是,您可以按n查找下一次出现)

同样,当你给出一个源(模式)并在替换命令中保留替换时,它假定替换是空的,因此给定的模式被替换为没有字符(换句话说,模式被删除)< / p>

在您的情况下,您应该了解%代表整个文件(缓冲区)和s代替。要进行搜索,您只需使用/,然后使用模式即可。要替换,您将使用:s。你不必混淆搜索和替代。因此,〜/ .exrc中不需要这样的设置。另外,请记住/足以搜索整个缓冲区,而%不需要//隐式搜索整个缓冲区。

您可能还想查看:g/Pattern/。通过在命令行中搜索:help global:help :g来了解有关它的更多信息。

答案 1 :(得分:2)

The format of a substitution in vim is as follows:

:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

In your case you have omitted the string from the substitution command and here what vim documentation stated about it:

If the {string} is omitted the substitute is done as if it's empty. Thus the matched pattern is deleted. The separator after {pattern} can also be left out then. Example: > :%s/TESTING This deletes "TESTING" from all lines, but only one per line.

For compatibility with Vi these two exceptions are allowed: "/{string}/" and "\?{string}?" do the same as "//{string}/r". "\&{string}&" does the same as "//{string}/".

E146

Instead of the '/' which surrounds the pattern and replacement string, you can use any other single-byte character, but not an alphanumeric character, '\', '"' or '|'. This is useful if you want to include a '/' in the search pattern or replacement string. Example: > :s+/+//+

In other words :%s/something and :%s;something or :%s,something have all the same behavior because the / ; and , in the last examples are considered only as SIMPLE SEPARATOR