How do I open the file path under my cursor in a new split window?
Answer
<C-w>f
Explanation
How it works
You may already know that gf opens the file path under the cursor in the current window. But sometimes you want to keep the current file visible while also viewing the referenced file. That is where <C-w>f comes in.
<C-w>f reads the filename under the cursor and opens it in a new horizontal split. The current window remains unchanged, and the referenced file appears in a new window above it.
For a vertical split instead, use <C-w>F or the command :vertical wincmd f.
Vim uses the path option to search for the file. You can see and modify this with :set path?. A common setting is :set path+=** which makes Vim search recursively through subdirectories.
Example
Suppose you have a Python file that contains an import statement:
from utils.helpers import format_date
Place your cursor on utils/helpers (or utils.helpers if you have suffixesadd and includeexpr configured), then press <C-w>f. Vim opens utils/helpers.py in a split above:
+----------------------------+
| utils/helpers.py |
+----------------------------+
| main.py (cursor was here) |
+----------------------------+
This is especially handy when reading code that references other files -- you can quickly peek at the definition without losing your place in the current file. Press <C-w>c to close the split when you are done.