Open to a pattern

4.1
(219)

In a recent VimTrick, we covered opening Vim to a specific line. Today’s trick is a slight variation on this topic that came up in discussion after that post – one that I am already finding uses for in my workflow today. It’s possible to open Vim to the first occurrence of a pattern, right from the command line. It uses a similar command line syntax:

vim +/your_pattern some_file.name 

Argument order doesn’t matter, it works the other way, too:

vim some_file.name +/your_pattern 

Some might point out that this method uses an extra keystroke compared to jumping to the pattern after Vim loads with /pattern<Enter>. But a distinct advantage to opening it from the command line is the ability to repeat this open/jump combination when editing the file again. Remember, in your shell, ctrl-r does a reverse search through your shell history, giving you quick access to edit this same section of this same file again.

I find this useful for editing config files or .env files. If I need to commonly change a certain environment variable, opening the file to the pattern from the command line allows me quick access from my shell history.

Here’s a screencast of this in action:

How useful was this tip?

Average rating 4.1 / 5. Vote count: 219

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

Andy Libby

30 Posts

Rider of bicycles. Writer of code. User of Vim.
View all posts

Leave a reply

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