Operate until pattern

4.5
(210)

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 first X. The X can 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 X.

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 until foo is found
  • c/bar<Enter> – change everything up until bar
  • y/pattern<Enter> – yank everything up until pattern

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:

How useful was this tip?

Average rating 4.5 / 5. Vote count: 210

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

81 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

Leave a reply

Your email address will not be published. Required fields are marked *