18

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/

:%s/word/newword/count

And other commands that i could find on google. But none of them works.

EDIT:: Vim commands is preferred. But Vim script can also be used. I don't have any prior experience in vim scripting.

1
  • 1
    :s/word/newword/N is the only valid command, but that would replace the first occurrence on the next N lines. And if you used the /g flag, it'll replace all occurrences on the next N lines. Neither do the the exact count you desire.
    – Caek
    Sep 15, 2014 at 5:48

4 Answers 4

24

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

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

See :help recording and :help gn.

Another way, using :normal:

:norm! /foo<C-v><CR>cgnbar<C-v><Esc>     <-- should look like this: :norm! /foo^Mcgnbar^[
11@:

See :help :normal and :help @:.

Or simply:

:/foo/|s//bar<CR>
11@:
4
  • @romainl Can you explain meaning of each line and attribute?
    – Ignited
    Sep 15, 2014 at 12:45
  • 1
    @Ignited, I've added :help pointers. Ask me if you have questions on specific parts of my answer.
    – romainl
    Sep 15, 2014 at 12:54
  • Wow what a clever use of cgn . Any specific resource where I can get started with such commands and vim scripting too?
    – Ignited
    Sep 15, 2014 at 14:34
  • +1. gn also goes great with the .. Can skip patterns with n. Sep 15, 2014 at 19:56
12

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.

3
  • 7
    :call feedkeys(repeat("y", 3) . "q") | %s/test/toast/gc for a specific amount. Where 3 is the number of occurrences to replace.
    – Caek
    Sep 15, 2014 at 5:55
  • I laughed a little when I realized I still had my test/toast in there from testing, but you get the point :)
    – Caek
    Sep 15, 2014 at 6:53
  • This solution is easy both to remember and to read! Very elegant! I like it way more than a "complex succession of cryptic vim magic characters"... ;) Thank you!
    – Hub 20xx
    Sep 14, 2018 at 8:46
7

I'm not sure about specifying the first N occurrences, but I often use this command:

:%s/word/newword/gc

Vim then asks for confirmation of each occurrence of word so you can selectively change some but not others.

3
  • 1
    Will be quite tedious and high chances of human counting error when N is large
    – Ignited
    Sep 15, 2014 at 4:02
  • Yes, that's true. But when N is not large, it gives a lot more control over what is changed. Sep 15, 2014 at 4:03
  • Also, this is an easier to understand command than the others proposed in this thread.
    – Moebius
    Oct 4, 2017 at 16:04
2

My PatternsOnText plugin provides (among many others) a command that takes answers in the form of either yyynyn or 1-5:

:%SubstituteSelected/word/newword/g 1-5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.