VIM: Finding and replacing first N occurrences of a word

前端 未结 4 1508
一向
一向 2020-12-28 08:39

I am editing a file and i want to change only a specific word with another word, but only for first N occurrences. I tried multiple commands

N :s/word/newword/

4条回答
  •  情歌与酒
    2020-12-28 08:58

    Using a disposable recording allows you to control exactly how many changes you do:

    qq             " start recording in register q
    /foo       " search for next foo
    cgnbar    " change it to bar
    q              " end recording
    11@q           " play recording 11 times
    

    See :help recording and :help gn.

    Another way, using :normal:

    :norm! /foocgnbar     <-- should look like this: :norm! /foo^Mcgnbar^[
    11@:
    

    See :help :normal and :help @:.

    Or simply:

    :/foo/|s//bar
    11@:
    

提交回复
热议问题