Open file in a split
Vim provides a mapping of
gf to go to the file under the cursor. Whether it’s a class name or a file path, Vim can usually resolve it a file under your present working directory and take you directly to that file. This is one of the primary ways I navigate around a codebase.
But there’s a variation of this which will open the file under the cursor in a split:
<ctrl-w> f– Open the file under the cursor in a new horizontal split
<ctrl-w> F– Also go to the referenced line number if provided
The capitalized version,
<ctrl-w> F, is useful in cases when you have a filename appended with a line number. This often appears in the context of a test or compiler failure in the quickfix window. (Are you using Tim Pope’s Dispatch?) Vim will recognize many line numbering conventions, including all of these cited specifically in the docs:
eval.c:10 eval.c @ 20 eval.c (30) eval.c 40
The horizontal split is definitely useful. But if your screen real estate is anything like mine, you might prefer to open in a vertical split rather than a horiztonal split. Although Vim doesn’t provide this capability out of the box, you could remap
<ctrl-w> f to use vertical splits with a mapping as follows inside your
map <C-w>f <C-w>vgf
This tells Vim to open a vertical split first, then call
gf, which goes to that file. You could also map this to something else if you felt the need to have both variations available. (If that’s you, let us know in the comments what mapping you use for this!)
Here’s a quick demo I made of this in action:
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?