I'm looking for a function(probably doesn't exist) which can return the filepath under the cursor. I know that there is a command/shortcut gf that helps me jump to that file if the file exists.

But I do wonder, is there a function so that I can simply get that filename/filepath without opening it? If not, that function might be very useful in some cases. Are there any elegant ways to implemente the function(use regex?)?

As far as I know, the only way seems using pattern matching. However, the regex and the matching logic seems a little complex(suppose there are multiple filenames in the line which the cursor locates at)...

1 Answer 1


Yes, expand(.) with argument '<cfile>':

expand({expr} [, {nosuf} [, {list}]])               *expand()*
        Expand wildcards and the following special keywords in {expr}.
        'wildignorecase' applies.
        When {expr} starts with '%', '#' or '<', the expansion is done
        like for the |cmdline-special| variables with their associated
        modifiers.  Here is a short overview:
            <cfile>     file name under the cursor

  • It works! Thank you very much and sorry for my stupid question...
    – WW00WW
    Sep 17, 2020 at 2:59
  • And I ever thought expand('<cfile>') only returns the path of the current buffer... I am wrong...
    – WW00WW
    Sep 17, 2020 at 2:59

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.