Vim:搜索模式并在每个匹配行的末尾添加文本

7
我想在vim中搜索一个模式,并在出现该模式的每一行的末尾添加文本。例如,如果搜索模式是print(,要添加的文本是
from __future__ import print_function
print('Pausing 30 seconds...'
print("That's not a valid year!"

should become

from __future import print_function
print('Pausing 30 seconds...')
print("That's not a valid year!")

同时,将光标放在“print”上,按 *A)n. 以重复至厌倦。 - sehe
可能是 Globally append to line with matching term in vim 的重复问题。 - kenorb
2个回答

12

这个命令应该能帮到你:

:g/print(/norm! A)

它的作用:

:g/print(/   "find all lines matching the regex
norm! A)     "do norm command, append a ")" at the end of the matched line.

你可能想要检查

:h :g

了解详情请查看。


1
要在以特定字符串开头的行末添加文本,请尝试:
:g/^print(/s/$/)

请参阅: g 的幂 - 示例 以获取更进一步的解释。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接