例如,假设我想将第 1、3、11 和 15 行的 foo
替换为 bar
。我该怎么做?
:1,15s/foo/bar
将第 1-15 行的 foo
替换为 bar
。但我想指定多个单独的行 (1,3,11,15),而不是一个范围 (1-15)。
最佳答案
一种方法:对第一行执行 :substitute
,然后在后面的行中用 :&&
重复相同的替换。
:execute '1s/foo/bar' | 5&& | 11&& | 15&&
另一种方法:使用 :global
命令和仅在行中匹配的模式。
:g/\%1l\|\%5l\|\%11l\|\%15l/s/foo/bar
第三种方式:使用循环:
:for l in [1,5,11,15] | execute l.'s/foo/bar' | endfor
关于vim - 如何在非连续行上运行命令行命令,指定单独的行号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13288685/