Search and replace in vim in specific lines

后端 未结 5 1287
时光取名叫无心
时光取名叫无心 2020-12-07 13:10

I can use

:5,12s/foo/bar/g

to search for foo and replace it by bar between lines 5 and 12. How can I do that only

5条回答
  •  -上瘾入骨i
    2020-12-07 13:46

    You can do the substitution on line 5 and repeat it with minimal effort on line 12:

    :5s/foo/bar
    :12&
    

    As pointed out by Ingo, :& forgets your flags. Since you are using /g, the correct command would be :&&:

    :5s/foo/bar/g
    :12&&
    

    See :help :& and friends.

提交回复
热议问题