Open to a pattern
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:
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?