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

本文介绍了如何在VIM编辑器中使用简单命令将当前打开文件的完整路径复制到剪贴板,并提供了实际应用场景的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值