Assuming the current buffer is a file open for edit, so :e
does not display E32: No file name
.
I would like to yank one or all of:
~\myfile.txt
c:\foo\bar\myfile.txt
myfile.txt
If all that is wanted is to display the name of the current file, type Ctrl-G (or press 1 then Ctrl-G for the full path). When using @% , the name is displayed relative to the current directory. In insert mode, type Ctrl-R then % to insert the name of the current file.
Pressing 1 followed by Ctrl + G shows the full path of the current file.
TL;DR
:let @" = expand("%")
>
this will copy the file name to the unamed register, then you can use good old p
to paste it. and of course you can map this to a key for quicker use.
:nmap cp :let @" = expand("%")<cr>
you can also use this for full path
:let @" = expand("%:p")
Explanation
Vim uses the unnamed register to store text that has been deleted or copied (yanked), likewise when you paste it reads the text from this register.
Using let
we can manually store text in the register using :let @" = "text"
but we can also store the result of an expression.
In the above example we use the function expand
which expands wildcards and keywords. in our example we use expand('%')
to expand the current file name. We can modify it as expand('%:p')
for the full file name.
See :help let
:help expand
:help registers
for details
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With