1

I'm using NVIM 0.8.2

I'd like to replace the default gf command (goto file under the cursor) to do something before and after the action.

For example:

echo "do something before"
goto file/dir under cursor
echo "do something after"

I can't find what command gf is calling, how can I do this?

4
  • 1
    gf doesn't "call" any "command", that's not how it works. gf is the "command". f you want to override gf, 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.
    – romainl
    Jan 24 at 18:10
  • I made a few more dig with the problem what I'm trying to solve, I think the issue is not on gf, but my pwd changes when I gf or :e to a folder, like :e ./src, so my pwd become <projectPath>/src instead of <projectPath>. (I have autochdir off)
    – Lai32290
    Jan 24 at 18:25
  • 2
    I have seen quite a few Neovim users complain about arbitrarily changed working directory recently. I don't use that thing, though, so I will stop here but others might be interested to know what file manager you are using.
    – romainl
    Jan 24 at 19:28
  • At any rate, to run the gf command, you can use :normal! gf
    – D. Ben Knoble
    Jan 25 at 14:43

1 Answer 1

2

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.

3
  • 1
    nnoremap gf :<c-u>call Beforegf()<cr>gf:<c-u>call Aftergf()<cr> since nnoremap helps prevent the inner gf from recurring into the mapping, and after :…<CR> we're in normal mode…
    – D. Ben Knoble
    Jan 26 at 14:43
  • Wow! I think both way works for me? And I learned new thing here (norm! gf)
    – Lai32290
    Jan 26 at 22:39
  • @ Lai, glad to help. @D.BenKnoble, hah, thanks...that was really silly on my part. Will edit my answer.
    – Wilson
    Jan 27 at 0:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.