I'm pretty sure you can't do that in vim, but to make it less tedious, do you know about "n" and "." in visual mode? That is, you use /pattern/ to find the thing to change, change it using "cw" or whatever, thn use "n" (just n, no quotes) to find the next pattern and hit "." (just period, no quotes) to perform the last edit.
–
Bruce EdigerDec 19 '11 at 19:55

@BruceEdiger I did know about n and ., though I didn't think to use them here. Certainly an improvement, thanks.
–
KevinDec 19 '11 at 20:02

s/a/=something/ should do the trick (:help sub-replace-=). I'm not fluent enough in Vim to write something right off the bat.
–
GillesDec 19 '11 at 23:29

Note: :[up][enter] can be replaced with &, which still isn't ideal but at least is less painful.
–
KowhDec 20 '11 at 0:14

3 Answers
3

Building on the :s/pattern/replacement/gcidea from Samus_ (which seems to be the simplest way to ensure correct operation when pattern is contained within the replacement string), to replace the 2nd through 4th occurrences on a single line:

:call feedkeys("nyyyq") | s/pat/string/gc

feedkeys() is a function that stuffs the input string into the keyboard input queue. The point is to do the counting upfront so you don't have to worry about losing count or getting interrupted.

For a more general case, to replace the Mth through Nth occurrences on a single line for N greater than or equal to a very large M:

Replace M and N with the values you want (you can even let vim do the trivial mental arithmetic if you don't want to do it yourself). Note that . is VimL's string concatenation operator. Obviously this only saves keystrokes for large M. If you use this functionality frequently, it may save you time to put the above in a custom command or function of some sort, as it is quite a bit to type.

Hmm, I forgot about c, that might be the best solution proposed yet. I'd still have to count, but I think it's the first option that would work with replacements that contain the search string.
–
KevinDec 20 '11 at 16:56

This is good, but it still suffers from the problem Gilles pointed out (on another post, which since seems to have been deleted), that it only works if the replacement doesn't contain the pattern. Though I do like the scripting idea.
–
KevinDec 20 '11 at 15:46