I've the following example line:

aa ... abc abc abc ... zz

I'm aware that :s/abc/ok/ will change the first occurrence of abc in the current line and adding g flag will change all of them.

How do I change last occurrence of the string (without knowing how many there are and what's after the last)?


1 Answer 1


Greedy matching:


The .* will consume as many characters as possible (therefore leaving you at the last occurrence), and the \zs sets the start of the actual match (\zs and \ze are useful substitutes for positive lookaheads/lookbehinds in Vim) so that the text before isn't removed as well.

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