vim - clipboard yank file name/path of current buffer in VIM

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

  1. Type :let @" = expand("%:p") (The path gets yanked to the main clipboard buffer.)

  2. Open a new window with :sp

  3. Type :e Ctrl+R"

  4. 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章