简体   繁体   English

在Vim中编写替换正则表达式模式时如何调用搜索模式?

[英]How to recall search pattern when writing replace regex pattern in Vim?

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. 我通过使用/命令并查看其突出显示内容来摆弄并提出完美的搜索模式。

Now I want to use that pattern to replace with. 现在我想使用该模式替换。 So, I start typing :%s/ and I cannot recall what the pattern was. 所以,我开始输入:%s / ,我不记得模式是什么。 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. 如果我正在编写一个特别复杂的正则表达式,我甚至打开了一个新的MacVim窗口,从第一个窗口输入正则表达式到那里的缓冲区,然后在写入替换模式时将其键入Vim窗口。 There has got to be a better way of doing so. 必须有一个更好的方法。

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提示将其列为替代最后搜索 ,可以通过两种方式之一完成。

  1. Using an empty search: 使用空搜索:

    :%s//replace/g :%S //替换/克

  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然后按/从搜索缓冲区中调用材料( / - 您可以使用/替换表示该缓冲区的字母来使用任何其他命名缓冲区)。

有一个包含当前搜索模式的/ register,因此您可以使用<CTRL-R>/将搜索模式插入到您正在键入的任何内容中(在插入模式下或在命令行上)

Another option: after entering the pattern with <CTRL-R>/ , edit the command line with <CTRL-F> . 另一个选项:使用<CTRL-R>/输入模式后,使用<CTRL-F>编辑命令行。

:help cmdwin :help cmdwin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM