When I exchange with colleagues about the code, when I refer to given functions, I like to also mention the file.

Since this exchange does not usually take place within vim, I usually copy them out using the * register.

It's easy to copy the name of the function (typically, I would use "*yiw). However, for the file name, I can only think of using the command line as

:let @*=expand('%:t')

So I know that I can map that to a command. But is there a built-in way to achieve that?

And maybe there is a way to set to any register, which would not be available with a fixed mapping to the line above?

  • use use the builtin % register. It contains the filename for the current buffer, but you may still need to run expand() on it, if you just want to have the full path or just the final filename Commented May 31, 2022 at 17:49
  • Ok, but how do I copy the content of % onto *? Commented May 31, 2022 at 18:32
  • 2
    let @*=@% ? Commented May 31, 2022 at 18:36
  • See @VivianDeSmedt comment Commented Jun 1, 2022 at 7:23
  • I do as @VivianDeSmedt suggests; at that point, I can trim needless leading components (likes /Users/…/) later if need be, but since pwd is usually the project root @% is usually the most unambiguous path reference.
    – D. Ben Knoble
    Commented Jun 1, 2022 at 7:49


Your Answer

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

Browse other questions tagged or ask your own question.