在Vim中交互式搜索/替换regex ?

时间:2020-12-22 16:49:44

I know the regex for doing a global replace,

我知道regex是做全球替换的,

     %s/old/new/g

How do you go about doing an interactive search-replace in Vim?

如何在Vim中进行交互式搜索替换?

6 个解决方案

#1


361  

Add the flag c (in the vim command prompt):

添加标记c(在vim命令提示符中):

:%s/old/new/gc

will give you a yes/no prompt at each occurrence of 'old'.

每次出现“老”时,都会提示你“是”或“否”。

在Vim中交互式搜索/替换regex ?

Vim's built-in help offers useful info on the options available once substitution with confirmation has been selected. Use:

Vim的内置帮助提供了一个有用的信息,一旦选择了替代,就可以选择。使用:

:h :s

Then scroll to section on confirm options. Screenshot below:

然后滚动到确认选项部分。截图:

在Vim中交互式搜索/替换regex ?

For instance, to substitute this and all remaining matches, use a.

例如,要替换这个和所有剩余的匹配,使用一个。

#2


59  

Mark Biek pointed out using:

Mark Biek指出:

%s/old/new/gc

for a global search replace with confirmation for each substitution. But, I also enjoy interactively verifying that the old text will match correctly. I first do a search with a regex, then I reuse that pattern:

对于全局搜索,替换为每个替换的确认。但是,我也喜欢交互地验证旧文本是否匹配正确。我首先使用regex进行搜索,然后重用该模式:

/old.pattern.to.match
%s//replacement/gc

The s// will use the last search pattern.

s//将使用最后一个搜索模式。

#3


17  

I think you're looking for c, eg s/abc/123/gc, this will cause VIM to confirm the replacements. See :help :substitute for more information.

我想你正在寻找c,如s/abc/123/gc,这将导致VIM确认替换。参见:帮助:替换更多信息。

#4


9  

If you just want to count the number of occurrences of 'abc' then you can do %s/abc//gn. This doesn't replace anything but just reports the number of occurrences of 'abc'.

如果你只是想计算“abc”的出现次数,那么你可以做%s/abc//gn。这并没有取代任何东西,只是报告了“abc”的出现次数。

#5


8  

I usually use the find/substitute/next/repeat command :-)

我通常使用find/ replace /next/repeat命令:-)

/old<CR>3snew<ESC>n.n.n.n.n.n.n.

That's find "old", substitute 3 characters for "new", find next, repeat substitute, and so on.

那就是找到“旧”,用3个字符替换“新”,找到下一个,重复替换,等等。

It's a pain for massive substitutions but it lets you selectively ignore some occurrences of old (by just pressing n again to find the next one instead of . to repeat a substitution).

这是一个巨大的替换的痛苦,但是它让你有选择地忽略一些旧的事情(通过再次按n来找到下一个)。重复一个替换)。

#6


2  

If your replacement text needs to change for each matched occurrence (i.e. not simply choosing Yes/No to apply a singular replacement) you can use a Vim plugin I made called interactive-replace.

如果你的替换文本需要为每一个匹配的事件改变(例如,不仅仅是选择Yes/No来应用一个单一的替换),你可以使用我做的一个Vim插件,叫做交互替换。

#1


361  

Add the flag c (in the vim command prompt):

添加标记c(在vim命令提示符中):

:%s/old/new/gc

will give you a yes/no prompt at each occurrence of 'old'.

每次出现“老”时,都会提示你“是”或“否”。

在Vim中交互式搜索/替换regex ?

Vim's built-in help offers useful info on the options available once substitution with confirmation has been selected. Use:

Vim的内置帮助提供了一个有用的信息,一旦选择了替代,就可以选择。使用:

:h :s

Then scroll to section on confirm options. Screenshot below:

然后滚动到确认选项部分。截图:

在Vim中交互式搜索/替换regex ?

For instance, to substitute this and all remaining matches, use a.

例如,要替换这个和所有剩余的匹配,使用一个。

#2


59  

Mark Biek pointed out using:

Mark Biek指出:

%s/old/new/gc

for a global search replace with confirmation for each substitution. But, I also enjoy interactively verifying that the old text will match correctly. I first do a search with a regex, then I reuse that pattern:

对于全局搜索,替换为每个替换的确认。但是,我也喜欢交互地验证旧文本是否匹配正确。我首先使用regex进行搜索,然后重用该模式:

/old.pattern.to.match
%s//replacement/gc

The s// will use the last search pattern.

s//将使用最后一个搜索模式。

#3


17  

I think you're looking for c, eg s/abc/123/gc, this will cause VIM to confirm the replacements. See :help :substitute for more information.

我想你正在寻找c,如s/abc/123/gc,这将导致VIM确认替换。参见:帮助:替换更多信息。

#4


9  

If you just want to count the number of occurrences of 'abc' then you can do %s/abc//gn. This doesn't replace anything but just reports the number of occurrences of 'abc'.

如果你只是想计算“abc”的出现次数,那么你可以做%s/abc//gn。这并没有取代任何东西,只是报告了“abc”的出现次数。

#5


8  

I usually use the find/substitute/next/repeat command :-)

我通常使用find/ replace /next/repeat命令:-)

/old<CR>3snew<ESC>n.n.n.n.n.n.n.

That's find "old", substitute 3 characters for "new", find next, repeat substitute, and so on.

那就是找到“旧”,用3个字符替换“新”,找到下一个,重复替换,等等。

It's a pain for massive substitutions but it lets you selectively ignore some occurrences of old (by just pressing n again to find the next one instead of . to repeat a substitution).

这是一个巨大的替换的痛苦,但是它让你有选择地忽略一些旧的事情(通过再次按n来找到下一个)。重复一个替换)。

#6


2  

If your replacement text needs to change for each matched occurrence (i.e. not simply choosing Yes/No to apply a singular replacement) you can use a Vim plugin I made called interactive-replace.

如果你的替换文本需要为每一个匹配的事件改变(例如,不仅仅是选择Yes/No来应用一个单一的替换),你可以使用我做的一个Vim插件,叫做交互替换。