Sometimes it is necessary to yank the name of the current filename/path of the current buffer so we can view/paste the name it to somewhere. here is some tips that shows you how .
From the stackoverflow post here: clipboard yank file name/path of current buffer in VIM.
One of the tip is like this:
Try this:
: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("%")
you can also use this for full path
:let @" = expand("%:p")
use :help expand for more details
and below is one use case by an example.
Combining information from a couple of other answers: If you want to yank the current full path to a file and put it into the command buffer in another window, first do :let
@" = expand("%:p")
, then move to another window and type Ctrl+R
"
.
Useful for copying a file while staying in the same directory and keeping the old one open. For example:
Start: Editing src/com/benatkin/paint/shapes/Circle.java
Type
:let @" = expand("%:p")
(The path gets yanked to the main clipboard buffer.)Open a new window with
:sp
Type
:e Ctrl+R"
Use the arrow keys to go back to Circle and change it to Square, and press <CR>
End: Editing src/com/benatkin/paint/shapes/Square.java