vimtricks.wiki Concise Vim tricks, one at a time.

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 +/pattern is the most common form
  • The search pattern is set as the current search (so n/N continue 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 +/pattern as a Vim argument: vim +/TODO README.md
  • Combine with :args to open multiple files and land on a pattern in the first: :n +/pattern

Next

How do I use capture groups in Vim substitutions to rearrange or swap matched text?