【问题标题】:Vim: Search for patterns and add text to end of each line where it occursVim:搜索模式并将文本添加到它出现的每一行的末尾
【发布时间】:2021-11-07 02:16:28
【问题描述】:

我想在 vim 中搜索一个模式,并在它出现的每一行上,将文本添加到行尾。例如,如果搜索模式是print(,而要添加的文本是)

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

应该变成

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

【问题讨论】:

标签: search vim replace


【解决方案1】:

这个命令应该为你做:

: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

了解详情。

【讨论】:

    【解决方案2】:

    要将文本添加到以某个字符串开头的行尾,请尝试:

    :g/^print(/s/$/)
    

    更多解释请见:Power of g - Examples

    【讨论】:

      猜你喜欢
      • 2019-10-14
      • 2016-04-04
      • 2013-06-09
      • 2012-07-03
      • 1970-01-01
      • 2013-04-05
      • 2015-05-27
      • 2015-03-20
      相关资源
      最近更新 更多