In my daily Vim workflow I make regular use of c
, d
, and sometimes y
. When combined with t
, I can quickly delete or change the text on a line up unt
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.
First, a word from our sponsor
Well, a word from us. Tomorrow is the day! 🎈 We are launching our first email-based Vim course, Git Better with Vim. The format is similar to the VimTricks format you know and love: bite-sized tutorials delivered at regular intervals (24 of them) over email. But we’re doing a very deep dive into how to use Git from within Vim. We cover the most popular Git plugin, Fugitive, in great detail. But we cover many other plugins and workflows as well. This course is going to blow your mind! I had now idea how efficient I could be performing my daily Git workflows inside Vim.
In celebration of our launch, we are offering 30% off with promo code “launch”. We’d be thrilled if you joined us for this 12 week exploration of Git and Vim. We had a blast writing it and learned a ton and we are certain you will, too.
Okay, back to today’s regularly scheduled VimTrick… In the gif 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:
Was this useful? Help us improve!
With your feedback, we can improve VimTricks. Click a link to vote: