I like the functionality of gf but for certain files I would like to run some (non-Vim) code on the file prior to displaying it in my window. Is there a way to capture the filename under the cursor so it can be used in a shortcut?

For example, I want to create a shortcut that would run <custom_unix_script> <file> (where is under the cursor) and display those results in a new buffer or tab.

2 Answers 2


In addition to @Luc Hermitte's answer of using expand(<cfile>), you can also use the :! command to run a shell command. You could do something like the following:

nnoremap gf :execute "!<custom_unix_script> " . expand(<cfile>)
  • nnoremap: Creates a normal mode non-recursive mapping
  • :execute: Allows us to build the command on the fly
  • :!...: Runs the following command in your shell
  • expand(<cfile>): Uses the file-like word under your cursor

Note that this will do this for ALL files. If you want to filter by certain files you'll need to get a little bit more fancy. I'd recommend calling a function and doing a conditional on the output of expand(<cfile>).

See :h expand, :h <cword>, :h execute, and :h !cmd for more info.


The filename under the cursor can be obtained with expand(<cfile>). From there you can do whatever you wish.

If you want more precisions, you'll need to provide us more precisions.

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.