Question:

Search and replace next n words in vi

Aubrey: 02 February 2022

Possible Duplicate:
Vim :s replace first N < g occurrences on a line (https://unix.stackexchange.com/questions/27178/vim-s-replace-first-n-g-occurrences-on-a-line)

In vi, how do I search and replace a word's first n occurrences with a word, say "hello", and the next m occurrences with bonjour, and all the rest with namaste?

Answer:
Claire: 02 February 2022

Can you locate the nth "hello" manually? If so, then, I would locate the nth hello as follows:

:1 (goes to the first line of your file)
n/hello (find the nth hello, where n is the number)

Then substitute all the hellos as follows:

:1,.s/hello/bonjour/g
(move to the next line)
:.,$s/hello/namaste/g