How do I open a file and automatically jump to the first line matching a pattern?
Answer
:e +/pattern filename
Explanation
The +{cmd} flag on :edit (and most file-opening commands) runs an Ex command immediately after the file loads. Using +/pattern runs a search, so the cursor lands on the first match the moment the file opens — no manual searching required.
How it works
:e +/{cmd} {file}— opens{file}and runs/{cmd}as a search right after loading- The
+flag accepts any Ex command, but+/patternis the most common form - The search pattern is set as the current search (so
n/Ncontinue from there) - Also works with
:split,:vsplit,:tabedit, and:badd
Example
Open a log file and jump straight to the first ERROR line:
:e +/ERROR /var/log/app.log
Open a config file and land on the host setting:
:vsplit +/^host server.conf
Jump to a specific function in a source file:
:tabedit +/def\ authenticate auth.py
(Escape spaces in the pattern with \ when the pattern contains spaces.)
Tips
- Use
+{linenum}to jump to a specific line number instead::e +42 main.go - Chain multiple commands with
+using bar notation — though only one+is allowed per:e, you can use:e +"cmd1 | cmd2" file - When opening from the shell, pass
+/patternas a Vim argument:vim +/TODO README.md - Combine with
:argsto open multiple files and land on a pattern in the first::n +/pattern