3

If the cursor is at t in

the quick brown fox

and we type d/brown, we delete the quick . Is there a way to extend this such that the searched term is deleted too? (Such that only fox remains.)

1 Answer 1

4

Searches are exclusive by default. That means that you only delete up to the start of the matched text which is where the cursor would end up. We need some way to force the match/cursor to fall at a point that encompasses the whole pattern string. One way is to add the offset flag /e:

d/brown/e

With no numerical value specified after /e the offset is effectively 0 (from the flag's position) and the cursor falls on the last character of brown. Thus everything up to and including that character gets deleted. (Whereas, for example, /e+2 (or more) would put the cursor past the pattern.)

Take a look at :h search-offset for details about /e and several other flags with related functions.

Alternatively, you can use the more familiar \zs to do accomplish the same thing:

d/brown\zs

This pattern atom "sets the start of the match" to its location. Since everything up to the start of the match is deleted this also has the the result we want.

See :h /\zs for details.

5
  • 1
    :help search-offset lists a myriad of useful options
    – D. Ben Knoble
    Dec 16, 2020 at 15:53
  • 1
    Yeah...still writing...usually put help links last. :)
    – B Layer
    Dec 16, 2020 at 15:53
  • 1
    Thank you both very much for the answers!
    – flawr
    Dec 16, 2020 at 16:04
  • Yeah, I just verified that the above is correct. What implementation are you using that's doing it wrong. That's very strange!
    – B Layer
    Dec 16, 2020 at 16:09
  • Oh, yeah. No suprise there. That's a Vim emulator (no actual Vim code in it) and that's a well known bug (some claim it's a feature). Been like that for years. I'm somewhere in the comments under the bug report complaining about it. :)
    – B Layer
    Dec 16, 2020 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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