In a line, how can Vim match only the last occurrence of a search pattern? Sort of like non-greedy, but from beginning.
e.g. In the following line, I want to match Twinkle little star
(highlighted in bold):
Twinkle blah Twinkle blah blah Twinkle little star.
I tried negative lookahead like the following, but it is matching the full line:
Twinkle.*\(Twinkle\)\@!$
vi positions the cursor at the next occurrence of the string. For example, to find the string “meta,” type /meta followed by Return. Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.
If you press "F", Vim will move the cursor backwards instead of forward. Given the previous sentence, if pressed "Fq", and the cursor was at the end of the line, it would move to the "q" in "quick".
Escape your parentheses and add a wildcard match before the anchor:
Twinkle\(.*Twinkle\)\@!.*$
^ ^ ^^
/\v.*\zsTwinkle.*
Use ":set incsearch", load up some text into a buffer, and start typing the pattern to see how it's applied.
For example, a buffer:
Twinkle brightly little star
Twinkle brightly, Twinkle brightly little star
And substitution:
:%s/\v.*\zsTwinkle /&extra /
Gives:
Twinkle extra brightly little star
Twinkle brightly, Twinkle extra brightly little star
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With