如何将“orm{ some string }”替换为“|一些字符串|' 很快



>我正在使用 vim 编辑一个降价文件。此文件存在许多字符串norm{ some string }。我想用| some string |替换它们.有没有快速的方法?非常感谢。

在多行的 vim 中查找和替换字符串中的答案无法回答我的问题。它只是谈论一行和多行的一般替换。在这里,我想替换周围的字符串并将字符串保留在周围。

您要查找的是所谓的捕获组和反向引用。如果没有嵌套形式(内部的大括号本身并不意味着问题(和跨越多行的表单,快速的解决方案可能是:%s/\norm {(.*)}/\|1\|/g .

替换部分的1是指被(.*)捕获的组,即最外层的一对大括号内的原始内容。例如,请参阅 http://www.vimregex.com/#backreferences 了解更多信息。

您还可以使用宏来完成所需的操作。

将光标放在具有要替换的模式的第一行上。然后开始录制宏:

qq0ldwr|$xi|ESCjq

意义:

qq  = start recording a macro (q) in register q
0   = move to the beginning of the line
l   = move one char to the right
dw  = delete the word
r|  = substitute what is under the cursor with a "|"
$   = move to the end of line
x   = delete last char of the line
i   = insert mode
|  = insert chars "|"
ESC = exit insert mode
j   = move to next line
q   = stop recording

使用以下命令执行宏:

@q

再次执行宏:

@@

根据需要继续执行尽可能多的行,或使用:

<number>@@
ex. 100@@

执行宏次数。

最新更新