Alex Rivera | Logout

Substitute the n-th occurrence of a word in vim

Asked 2010-06-19T00:39:59.780
21

I saw other questions dealing with the finding the n-th occurrence of a word/pattern, but I couldn't find how you would actually substitute the n-th occurrence of a pattern in vim. There's the obvious way of hard coding all the occurrences like

:s/.*\(word\).*\(word\).*\(word\).*/.*\1.*\2.*newWord.*/g 

Is there a better way of doing this?

Edit
Report

1 Answer

0

Well, if you do /gc then you can count the number of times it asks you for confirmation, and go ahead with the replacement when you get to the nth :D

answered 2010-06-19T00:43:51.067

Your Answer