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

How do I open a file under the cursor in a new split and jump to its line number in Vim?

Answer

<C-w>F

Explanation

<C-w>F opens the filename under the cursor in a new horizontal split window and jumps to the line number that follows the filename. This makes it ideal for navigating stack traces, compiler error output, or any text where filenames appear alongside line numbers (e.g. src/main.c:42).

How it works

  • <C-w>F is the uppercase variant of <C-w>f
  • Like <C-w>f, it opens the file under the cursor in a new horizontal split
  • Unlike <C-w>f, it also parses the line number that immediately follows the filename in the form filename:lineno or filename(lineno), and positions the cursor at that line

Example

Given a compiler error logged in a buffer:

src/parser.c:87: error: undeclared identifier 'tok'

With the cursor anywhere on src/parser.c:87, pressing <C-w>F will:

  1. Open src/parser.c in a new split
  2. Jump to line 87

Compare with the related commands:

<C-w>f  — open file in split (no line number)
gF      — open file + line number in current window (no split)
<C-w>gF — open file + line number in new tab

Tips

  • Vim uses the path option to resolve relative filenames, so set :set path+=src/** if needed
  • For filenames without a line number, <C-w>F falls back to opening the file at line 1
  • Use :set suffixesadd+=.c,.h to help Vim find files without explicit extensions

Next

How do I encode and decode JSON data in Vimscript for configuration and plugin development?