Try this:
function Beforegf()
"do something here
endfunction
function Aftergf()
"do something here
endfunction
nnoremap gf :<c-u>call Beforegf()<cr>gf:<c-u>call Aftergf()<cr>
if Beforegf()
is something that will move your cursor, you'll need to adjust accordingly. Example:
function Beforegf()
" yank the WORD under the cursor into the f register
norm! "fyiW
pu='This is the line after the filename'
endfunction
function Aftergf()
"do something here
endfunction
" Use the :e command with the contents of the f register to get the filename we yanked
nnoremap gf :<c-u>call Beforegf()<cr>:<c-u>exe 'e '.@f<cr>:<c-u>call Aftergf()<cr>
At any rate, you can use the normal command yiW
followed by the Ex command execute 'edit '.@"
as a rough equivalent of gf
. I would warn, however, that a WORD is not the exact same thing as a filepath. If you want a really robust solution, take another look at :help gf
and possibly incorporate the contents of the isfname
option into your method of determining what to yank.
gf
doesn't "call" any "command", that's not how it works.gf
is the "command". f you want to overridegf
, make a custom mapping that does what you want. A one line if that's enough or one that calls a custom function if not.gf
, but mypwd
changes when Igf
or:e
to a folder, like:e ./src
, so mypwd
become<projectPath>/src
instead of<projectPath>
. (I haveautochdir
off)gf
command, you can use:normal! gf