EDIT: Suggest nicely symmetric mappings for the vertical split case, since Vim by default has two mappings for the horizontal split case.
There are several ways to edit the "file under cursor", and while you certainly can remap gf to edit in a new tabpage as @alexander-myshov mentions, that means you have two ways to edit it in a new tabpage, but no way to edit it in the current window, which you may also sometimes want to do. It is not hard to use the standard mappings, but if you want to remap one of them to gf, I'd suggest switching the mappings, so that you don't lose one of them.
Some of the ways to edit a filename under the cursor are
- gf - Edit existing file under cursor in same window
- C-Wf - Edit existing file under cursor in split window
- C-WC-F - Edit existing file under cursor in split window
- C-Wgf - Edit existing file under cursor in new tabpage
There is no standard mapping to edit existing file under cursor in a vertical split, but you can achieve it by first splitting the window vertically and then editing the file under cursor in the current window.
- C-Wv+gf - Edit existing file under cursor in vertically split window
As you can see above, there are two default mappings for editing file under cursor in a horizontal split. You can therefore remap one of them to the keystring above, extending the group of maps to also cover the vertical split case. You can bind anything, of course, but this seems to complete the symmetry nicely: nnoremap <C-W><C-F> <C-W>vgf
. Now you have
- gf - Edit existing file under cursor in same window
- C-Wf - Edit existing file under cursor in split window
- C-WC-F - Edit existing file under cursor in vertically split window
- C-Wgf - Edit existing file under cursor in new tabpage
You can also go to a particular line in the file, if the filename under the cursor is followed by a line number. If you have
filename:10
you can edit the file and have the cursor move to line 10 with gF. The equivalent is true for C-WF for a split and C-WgF for a tabpage. Completing the symmetry for a vertical split is not as straightforward here, but I would suggest that the C-WC-S-F map obtained with nnoremap <C-W><C-S-F> <C-W>vgF
, imitating the F or Shift+f from the default maps as a candidate.
The :
is an example, you can use any character that is not a filename character (see :h 'isfname'
).
All these commands come with apple sauce, such as
- expand wildchars
- look in more places than the current directory, according to your
'path'
option
- adding a file extension to the filename, according to your
'suffixesadd'
option
- otherwise modify the given filname to find a match, according to your
'includeexpr'
option
Read more at :help gf
and the help topics for these various options.
If the filename under the cursor names a file that does not exist, you may be better of using the standard edit commands with <cfile>
, like
:edit <cfile> // edit non-existent file under cursor in current window
:split <cfile> // edit non-existent file under cursor in split window
:vsplit <cfile> // edit non-existent file under cursor in vertical split window
:tabe <cfile> // edit non-existent file under cursor in new tabpage
and with these of course you can use things like :spl+10 filename
to move the cursor to a particular line or :tabe+/cologne filename
to move the cursor to the first occurrence of a pattern.
'hidden'
option (set hidden
) allows you to navigate away from unsaved buffers. For me, this was a huge productivity booster, and was vital to stop relying on splits and tabs so much.:hide normal gf
and I guess also remapgf
to do this by default without settinghidden
map gf :hide edit <cfile><CR>
to override thehidden
and as a bonus, also make it work even if the file doesn't already exists