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

How do I open a file and immediately execute a command, like jumping to a pattern or line?

Answer

:e +{cmd} {file}

Explanation

The :e +{cmd} {file} form opens a file and executes an Ex command immediately after loading it. The + prefix is a powerful but underused feature for scripting file-open actions — jumping to a line, searching for a pattern, setting options, or running any Ex command without manual follow-up.

How it works

  • :e — opens (edits) a file in the current window
  • +{cmd} — any Ex command to run after the file is loaded (spaces within the command must be escaped with \)
  • {file} — the file path to open

Multiple + options can be combined. Special ++ flags handle file encoding and format.

Example

Open a large log file and jump directly to line 500:

:e +500 /var/log/app.log

Open a source file and search for the first main function:

:e +/main src/app.c

Open a script without an extension and set its filetype:

:e +set\ ft=sh deploy

Tips

  • Works with splits and tabs too: :sp +/TODO notes.txt, :tabedit +100 bigfile.py
  • Use ++ff=unix to open with a specific file format: :e ++ff=unix file.txt
  • Use ++enc=utf-8 to specify encoding: :e ++enc=utf-8 legacy.txt
  • Combine: :e ++ff=unix +/ERROR logfile opens with Unix line endings and jumps to the first ERROR
  • From the shell: vim +/pattern file uses the same + syntax on the command line

Next

How do I display the full absolute path of the current file in Vim?