gf edits the file whose name is under the cursor.

Sometimes I use gf to check if the file exist or not, by just going to the file and coming back. But if the file is large, then it takes time for opening it.

Is there a simple way in normal model to check the file whose name under the cursor does exist or not?

  • 1
    :h filereadable()
    – Maxim Kim
    Commented Feb 14, 2022 at 6:47

1 Answer 1


As Maxim Kim hints in the comments, you can try

:echo filereadable(expand('<cfile>')) ? 'exists' : 'does not exist'

According to :help <cfile>, this should work exactly like gf<C-o> in terms of the filename used.

  • Could you show me a few examples gf can handle, but the method cannot?
    – auditory
    Commented Feb 14, 2022 at 23:54
  • I ended up with map like nmap gG :echo filereadable(expand('<cfile>')) ? 'exists' : 'does not exist'<CR>. Thank you.
    – auditory
    Commented Feb 15, 2022 at 13:26
  • 1
    @auditory use nnoremap. It turns out <cfile> does all the things gf does, so never mind about that!
    – D. Ben Knoble
    Commented Feb 15, 2022 at 14:54
  • okay. I will use non-recursive mappig like :nnoremap .... Thanks.
    – auditory
    Commented Feb 20, 2022 at 14:06

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.