95

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
  • 4
    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, 2015 at 13:15
  • You can also :hide normal gf and I guess also remap gf to do this by default without setting hidden
    – CervEd
    Mar 18, 2023 at 18:01
  • So you can map gf :hide edit <cfile><CR> to override the hidden and as a bonus, also make it work even if the file doesn't already exists
    – CervEd
    Mar 18, 2023 at 22:11

3 Answers 3

139

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
  • +1 Best comment with clear explanation, far better than the documentation split across several topics. Dec 2, 2016 at 4:13
15

You can 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 you can check this help page: :help CTRL-W_gf

2
  • 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). Feb 22, 2016 at 14:35
  • Ah, nevermind - I have a plugin interfering. Feb 22, 2016 at 15:05
0

gf -> opens the file in a new tab

cf -> creates the file (if it doesn't exist) and opens it in new tab

nnoremap gf <C-W>gf 
noremap <leader>cf :call CreateFile(expand("<cfile>"))<CR>
function! CreateFile(tfilename)

    " complete filepath from the file where this is called
    let newfilepath=expand('%:p:h') .'/'. expand(a:tfilename)

    if filereadable(newfilepath)
       echo "File already exists"
       :norm gf
    else
        :execute "!touch ". expand(newfilepath)
        echom "File created: ". expand(newfilepath)
        :norm gf
    endif

endfunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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