41

The normal mode command gf go to a file with the path the cursor currently is at.

But it does so in the current window, replacing the current text. That means you need to navigate back explicitly to go to the back later, and that you need to write changes in the current buffer potentially.

I'm sure it's possible with opening the file under cursor in a new tabview instead?

  • 3
    Turning on the '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. – tommcdo May 22 '15 at 13:15
54

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.

  • +1 Best comment with clear explanation, far better than the documentation split across several topics. – Loves Probability Dec 2 '16 at 4:13
8

Just use bindings for this:

opening in a new window (split):

nnoremap gf <C-W>f
vnoremap gf <C-W>f

opening in a new tab:

nnoremap gf <C-W>gf
vnoremap gf <C-W>gf

also check this: :help CTRL-W_gf

  • Do these definitively work for you? Vim doesn't seem to want me to remap gf to something else. Even nnoremap gf :echo "hello"<CR> still opens the file under cursor in a new buffer (and yes, I sourced vimrc). – tandrewnichols Feb 22 '16 at 14:35
  • Ah, nevermind - I have a plugin interfering. – tandrewnichols Feb 22 '16 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.