在Vim中编写replace regex模式时如何回忆搜索模式?

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

Here's the scenario: I've got a big file filled with all sorts of eclectic rubbish that I want to regex. I fiddle around and come up with a perfect search pattern by using the / command and seeing what it highlights.

这里是这样的场景:我有一个很大的文件,里面塞满了各种各样的垃圾,我想要regex。通过使用/命令并查看它突出的地方,我摸索出了一个完美的搜索模式。

Now I want to use that pattern to replace with. So, I start typing :%s/ and I cannot recall what the pattern was. Is there some magical keyboard command that will pull in my last search pattern here? If I'm writing a particularly complex regex, I have even opened up a new MacVim window, typed the regex from the first window into a buffer there, then typed it back into the Vim window when writing the replace pattern. There has got to be a better way of doing so.

现在我想用这个模式来替换。所以,我开始键入:%s/,我不记得这个模式是什么。是否有一些神奇的键盘命令会拉进我最后的搜索模式?如果我正在编写一个特别复杂的regex,我甚至打开了一个新的MacVim窗口,将regex从第一个窗口输入到缓冲区中,然后在编写替换模式时将其输入到Vim窗口。必须有更好的办法来做到这一点。

3 个解决方案

#1


10  

Found my own answer after having written up the question: Vim tips lists it as substitute last search and can be done in one of two ways.

在写下这个问题后,我找到了我自己的答案:Vim tips将它作为最后搜索的替代,可以用两种方法之一完成。

  1. Using an empty search:

    使用一个空的搜索:

    :%s//replace/g

    :% s / /替换/ g

  2. By pressing Ctrl + r then / to recall the material from the search buffer (/ - you can use any other named buffer by substituting the / for the letter representing that buffer).

    通过按Ctrl + r然后/从搜索缓冲区中恢复材料(/ -您可以使用任何其他命名的缓冲区,方法是将/替换为表示该缓冲区的字母)。

#2


4  

There is a / register that contains the current search pattern, so you can insert the search pattern into whatever you are typing (either in insert mode or on the command line) with <CTRL-R>/

有一个/ register包含当前搜索模式,因此您可以将搜索模式插入到您输入的任何内容中(以插入模式或在命令行中),并使用 /

#3


1  

Another option: after entering the pattern with <CTRL-R>/, edit the command line with <CTRL-F>.

另一个选项:在输入 /后,使用 编辑命令行。

:help cmdwin

:帮助cmdwin

#1


10  

Found my own answer after having written up the question: Vim tips lists it as substitute last search and can be done in one of two ways.

在写下这个问题后,我找到了我自己的答案:Vim tips将它作为最后搜索的替代,可以用两种方法之一完成。

  1. Using an empty search:

    使用一个空的搜索:

    :%s//replace/g

    :% s / /替换/ g

  2. By pressing Ctrl + r then / to recall the material from the search buffer (/ - you can use any other named buffer by substituting the / for the letter representing that buffer).

    通过按Ctrl + r然后/从搜索缓冲区中恢复材料(/ -您可以使用任何其他命名的缓冲区,方法是将/替换为表示该缓冲区的字母)。

#2


4  

There is a / register that contains the current search pattern, so you can insert the search pattern into whatever you are typing (either in insert mode or on the command line) with <CTRL-R>/

有一个/ register包含当前搜索模式,因此您可以将搜索模式插入到您输入的任何内容中(以插入模式或在命令行中),并使用 /

#3


1  

Another option: after entering the pattern with <CTRL-R>/, edit the command line with <CTRL-F>.

另一个选项:在输入 /后,使用 编辑命令行。

:help cmdwin

:帮助cmdwin