Operate until pattern
In my daily Vim workflow I make regular use of c
, 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 firstX
. TheX
can be any character at all.ctX
– changes everything up until the firstX
. That is, deletes and leaves you in insert mode.ytX
– yanks (copies) everything up until the firstX
.
I recently discovered a more powerful way to use these three options d
, c
, y
: With a pattern rather than a single character. Simply use /some_pattern
and then <Enter>
after either d
, c
, or y
:
d/foo<Enter>
– delete up untilfoo
is foundc/bar<Enter>
– change everything up untilbar
y/pattern<Enter>
– yank everything up untilpattern
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: