8

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

kenorb
  • 18,433
  • 18
  • 72
  • 134

1 Answers1

11

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.

Doorknob
  • 15,237
  • 3
  • 48
  • 70