If I have opened in vim a file with the following contents


I currently have to type

:e bar/baz.html

to open that file for editing.

I'd prefer to have a command that automatically uses the current line.

Even better would be taking the visual selection instead of the whole line or even pattern matching since of course in the real file it looks like this

<a [...] href="bar/baz.html" [...]>

It should be possible to define a custom command for that in the .vimrc but I don't know how.

1 Answer 1


Put the target filename under the cursor and press gf. You can also do it with visual selection, such that, in the line below,

I want to open xfilex without the exes!

if you want to open file (not xfilex), then visually select file and press gf.

Find more information with :help gf:

[count]gf       Edit the file whose name is under or after the cursor.
                Trailing punctuation characters ".,:;!" are ignored. 
                Escaped spaces "\ " are reduced to a single space.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .