VIM: Finding and replacing first N occurrences of a word

前端 未结 4 1504
一向
一向 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 09:16

    Although a bit longer, you can do:

    :call feedkeys("yyyq") | %s/word/newword/gc
    

    to replace the first 3 occurrences and then stop. You can change the amount of y's for more or less replacements. (Can also use n to skip some)

    Explanation: this is feeding y keystrokes into the /c confirm option of the substitution command.

提交回复
热议问题