3

I have a bunch of text, say:

This is some sample text. I would like to delete this text.

The question is how do I delete till a specific word. If my cursor is at the start of the above sentence, and I say to delete till the word "text", this should be the result:

text. I would like to delete this text.

1 Answer 1

7

We can delete by searching (:h /):

d/text

It will wrap around the line and delete in any following line where "text" exists. If that is undesired then we have to resort to :h :substitute. (There are ways to use / to delete; see below.)

:s:\v.{-}\zetext::e

The above command will remove all characters preceding the word "text" from the current line. If the line does not contain the word "text," it will simply do nothing. For explanation, see :h :s, :h /\v, :h /\{-, :h /\ze, and :h :s_e.


Rich says, "You can still use a search if you only want to search on the current line, by matching the current cursor position: d/\%#.*\zstext. Or yet another technique: V<Esc>d/\%Vtext. This one works by only matching in the most recent visual selection."

4
  • 2
    Actually it will error, but the e flag (i think) suppresses that
    – D. Ben Knoble
    Oct 9, 2022 at 14:33
  • @D.BenKnoble, right; incorporated.
    – 3N4N
    Oct 9, 2022 at 14:50
  • 2
    You can still use a search if you only want to search on the current line, by matching the current cursor position: d/\%#.*\zstext
    – Rich
    Oct 10, 2022 at 13:32
  • 2
    Yet another technique! V<Esc>d/\%Vtext. This one works by only matching in the most recent visual selection. Vim is so cool.
    – Rich
    Oct 10, 2022 at 13:37

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.