Operate until pattern
In my daily Vim workflow I make regular use of
d, and sometimes
y. When combined with
t, I can quickly _ d elete or c hange the text on a line up un
t _il a character. For example:
dtX– deletes everything up until the first
Xcan be any character at all.
ctX– changes everything up until the first
X. That is, deletes and leaves you in insert mode.
ytX– yanks (copies) everything up until the first
I recently discovered a more powerful way to use these three options
y: With a pattern rather than a single character. Simply use
/some_pattern and then
<Enter> after either
d/foo<Enter>– delete up until
c/bar<Enter>– change everything up until
y/pattern<Enter>– yank everything up until
Watch the gif below for how this works. I’m definitely going to be using these on a regular basis now, especially for operations I want to repeat over and over on successive lines.
In the video below I make use of this pattern matching tip. Here I have a list of VimTricks and their publish date. Let’s say I want to remove everything up until the date. I can simply press
d/\d<Enter> to delete everything on the line up until the matched pattern, any digit. Then I can press
. on any of the next lines to repeat:
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?