208

假设当前缓冲区是打开编辑的文件,所以:e不显示E32: No file name.

我想拉出以下一项或全部:

  • 文件名与状态行显示的完全相同,例如~\myfile.txt
  • 文件的完整路径,例如c:\foo\bar\myfile.txt
  • 只是文件名,例如myfile.txt
4

10 回答 10

240

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

于 2009-06-05T05:01:12.853 回答
112

几乎是您所要求的,它可能会这样做:Ctrl+R %将当前文件名拉到您所在的位置(命令提示符,编辑缓冲区,...)。有关更多信息,请参阅此 Vim 提示

于 2009-05-27T17:05:55.550 回答
89

如果要将当前缓冲区文件名放在系统级剪贴板中,请尝试将寄存器更改为 @+:

" relative path
:let @+ = expand("%")

" full path
:let @+ = expand("%:p")

" just filename
:let @+ = expand("%:t")

编辑 20140421:我经常使用这些,所以我创建了一些快捷方式。Linux Vims 的操作显然与 Mac Vims 略有不同,因此也有一个特殊情况。如果您将以下内容放入您的~/.vimrc:

" copy current file name (relative/absolute) to system clipboard
if has("mac") || has("gui_macvim") || has("gui_mac")
  " relative path  (src/foo.txt)
  nnoremap <leader>cf :let @*=expand("%")<CR>

  " absolute path  (/something/src/foo.txt)
  nnoremap <leader>cF :let @*=expand("%:p")<CR>

  " filename       (foo.txt)
  nnoremap <leader>ct :let @*=expand("%:t")<CR>

  " directory name (/something/src)
  nnoremap <leader>ch :let @*=expand("%:p:h")<CR>
endif

" copy current file name (relative/absolute) to system clipboard (Linux version)
if has("gui_gtk") || has("gui_gtk2") || has("gui_gnome") || has("unix")
  " relative path (src/foo.txt)
  nnoremap <leader>cf :let @+=expand("%")<CR>

  " absolute path (/something/src/foo.txt)
  nnoremap <leader>cF :let @+=expand("%:p")<CR>

  " filename (foo.txt)
  nnoremap <leader>ct :let @+=expand("%:t")<CR>

  " directory name (/something/src)
  nnoremap <leader>ch :let @+=expand("%:p:h")<CR>
endif

然后例如<leader>cf将复制当前缓冲区的相对路径(默认领导者是反斜杠(\))。我经常使用这些来在文件上运行命令或在命令行上做其他事情。我真的不经常使用最后一个文件名/目录名。

您可能会考虑更直观的映射,例如<leader>cfr相对映射、<leader>cfa绝对映射、<leader>cff仅文件名映射、<leader>cfd目录映射。

于 2013-06-13T20:09:17.063 回答
7

如果这样做,您将在寄存器:reg中看到当前文件的名称。%例如,您可以使用 粘贴它"%p

如果像我一样,您经常切换到“备用”缓冲区,那么将其完整路径和文件名放入#寄存器非常方便。例如,您可以使用 粘贴它"#p

注意(以防这是我的设置特有的行为):我在 Ubuntu 14.04.4 LTS 上使用 VIM 7.4.52。

于 2016-03-03T09:42:16.923 回答
4

结合来自其他几个答案的信息:如果您想提取文件的当前完整路径并将其放入另一个窗口的命令缓冲区中,请先执行:let @" = expand("%:p"),然后移动到另一个窗口并键入Ctrl+R "

用于复制文件,同时保留在同一目录中并保持旧文件处于打开状态。例如:

开始:编辑src/com/benatkin/paint/shapes/Circle.java

  1. 类型:let @" = expand("%:p") (路径被拉到主剪贴板缓冲区。)

  2. 打开一个新窗口:sp

  3. 类型:e Ctrl+R"

  4. 使用箭头键返回 Circle 并将其更改为 Square,然后按 <CR>

结束:编辑src/com/benatkin/paint/shapes/Square.java

于 2010-05-04T03:11:26.630 回答
4

在 Neovim/Ubunutu 上测试的答案。

:let @+=@%

据我所知,%寄存器已经包含相对文件路径,因此只需将寄存器的内容移动%到代表您最喜欢的剪贴板的任何寄存器即可。

这个 SO 答案涉及从一个寄存器复制到另一个

对我来说似乎很简单。不需要任何难以记住的expand()东西。

于 2020-10-06T11:54:32.463 回答
1

这是我的解决方案:

" filename / dirname of the current file {{{
    " copy result to the system clipboard and echo the result
    " the cb> prompt means the clipboard
    " *f*ile *n*ame, ex. init.vim
    map <Leader>fn :let @+ = expand("%:t") \| echo 'cb> ' . @+<CR>
    " *f*ile *p*ath, ex. /home/user/nvim/init.vim
    map <Leader>fp :let @+ = expand("%:p") \| echo 'cb> ' . @+<CR>
    " *d*irectory *p*ath, ex. /home/user/nvim
    map <Leader>dp :let @+ = expand("%:p:h") \| echo 'cb> ' . @+<CR>
    " *d*irectory *n*ame, ex. nvim
    map <Leader>dn :let @+ = expand("%:p:h:t") \| echo 'cb> ' . @+<CR>
" }}}
于 2016-05-12T16:08:12.960 回答
1

如果您想简单地拉出当前打开文件名的路径,则只需按:

  1. step1: ctrl + g [你会在窗口底部看到整个根路径]
  2. step2:用鼠标选择路径
  3. step3:鼠标中键粘贴
于 2021-09-30T11:11:31.517 回答
0

我把它放在我的vimc中:

nnoremap yl :let @" = expand("%:p")<cr>
于 2022-03-01T15:20:17.867 回答
-1

我使用 xclip 来访问 X 的剪贴板,所以我使用:

nmap <localleader>d :call system("xclip -i -selection clipboard", expand("%:p"))<CR>
于 2013-10-02T20:44:42.160 回答