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

How do I open a file and jump directly to a specific line or pattern?

Answer

:e +{line} {file}

Explanation

The :edit command accepts a +{cmd} prefix that executes an Ex command immediately after the file is loaded. The most common use is +{line} to jump to a line number, but any Ex command works — including searches, marks, and substitutions.

How it works

  • :e +100 foo.c — open foo.c at line 100
  • :e +/pattern foo.c — open foo.c and jump to the first match of pattern
  • :e +$ foo.c — open foo.c and jump to the last line
  • :e +"call cursor(50,12)" foo.c — open at a specific line and column

The +{cmd} syntax also works with :split, :vsplit, :tabedit, and even the vim CLI (vim +100 file.txt).

Example

You see a compiler error referencing src/parser.c:247. Open straight to it:

:e +247 src/parser.c

Or jump to the definition of a function:

:e +/^int\ parse_token src/parser.c

Tips

  • Chain with :sp and :vs to open files in splits at a specific position: :sp +/TODO notes.txt
  • The +{cmd} option can include any single Ex command; for multiple commands chain with |: :e +"set ft=python | normal G" scratch.py
  • Useful for scripting workflows where you want to open related files at a known position

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?