4

This question already has an answer here:

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)?

marked as duplicate by Martin Tournoij May 24 '17 at 15:27

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

6

Greedy matching:

:s/.*\zsabc//

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.