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.)
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communitySearches 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.