Yim文件名/ Vim中当前缓冲区的路径

时间:2022-01-21 19:18:26

Assuming the current buffer is a file open for edit, so :e does not display E32: No file name.

假设当前缓冲区是一个打开以进行编辑的文件,因此:e不显示E32:没有文件名。

I would like to yank one or all of:

我想猛拉一个或全部:

  • The file name exactly as show on the status line, e.g. ~\myfile.txt
  • 完全如状态行所示的文件名,例如〜\ myfile.txt的

  • A full path to the file, e.g. c:\foo\bar\myfile.txt
  • 文件的完整路径,例如C:\富\酒吧\ myfile.txt的

  • Just the file name, e.g. myfile.txt
  • 只是文件名,例如myfile.txt文件

7 个解决方案

#1


TL;DR

:let @" = expand("%")>

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

这会将文件名复制到unamed寄存器,然后你可以使用旧的p来粘贴它。当然,您可以将其映射到一个键,以便更快地使用。

:nmap cp :let @" = expand("%")<cr>

:nmap cp:let @“= expand(”%“)

you can also use this for full path

你也可以用它来完整的路径

:let @" = expand("%:p")

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

Vim使用未命名的寄存器来存储已删除或复制(抽出)的文本,同样在粘贴它时会读取此寄存器中的文本。

Using let we can manually store text in the register using :let @" = "text" but we can also store the result of an expression.

使用let我们可以使用:let @“=”text“手动将文本存储在寄存器中,但我们也可以存储表达式的结果。

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.

在上面的例子中,我们使用扩展通配符和关键字的函数expand。在我们的示例中,我们使用expand('%')来扩展当前文件名。我们可以将其修改为扩展('%:p')以获取完整的文件名。

See :help let :help expand :help registers for details

请参阅:help let:help expand:help寄存器以获取详细信息

#2


Almost what you're asking for, and it might do: Ctrl+R % pulls the current filename into where you are (command prompt, edit buffer, ...). See this Vim Tip for more.

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

#3


If you want to put the current buffer filename in your system-level clipboard, try changing the register to @+:

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

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

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

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

Edit 20140421: I commonly use these, so I created some shortcuts. Linux Vims apparently operate slightly differently than Mac Vims, so there is a special case for that as well. If you put the following in your ~/.vimrc:

编辑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

Then for example <leader>cf will copy the relative path of the current buffer (the default leader is backslash (\)). I often use these for running commands on a file or doing other things on the command line. I don't really use the last filename / directory name often.

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

You might consider more intuitive mappings like <leader>cfr for relative, <leader>cfa for absolute, <leader>cff for just filename, <leader>cfd for directory.

您可以考虑更直观的映射,例如 cfr表示相对, cfa表示绝对, cff表示文件名, cfd表示目录。

#4


If you do :reg you will see the name of the current file in the % register. You can paste it with "%p, for example.

如果您执行:reg,您将在%寄存器中看到当前文件的名称。例如,您可以将其粘贴为“%p”。

If, like me, you often switch to the 'alternate' buffer, it is very handy that its full path-and-file-name are put in the # register. You can paste it with "#p, for example.

如果像我一样,你经常切换到'alternate'缓冲区,那么它的完整路径和文件名放在#register中是非常方便的。例如,您可以使用“#p粘贴它。

Note (just in case this is behaviour specific to my setup): I am using VIM 7.4.52 on Ubuntu 14.04.4 LTS.

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

#5


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

结合其他几个答案的信息:如果你想将当前的完整路径拉到一个文件并把它放到另一个窗口的命令缓冲区中,首先要做:let @“= expand(”%:p“),然后移动到另一个窗口并键入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

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

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

    键入:let @“= expand(”%:p“)(路径被拉到主剪贴板缓冲区。)

  2. Open a new window with :sp

    用:sp打开一个新窗口

  3. Type :e Ctrl+R"

    键入:e Ctrl + R“

  4. Use the arrow keys to go back to Circle and change it to Square, and press <CR>

    使用箭头键返回Circle并将其更改为Square,然后按

End: Editing src/com/benatkin/paint/shapes/Square.java

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

#6


I use xclip to access X's clipboard, so I use:

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

nmap <localleader>d :call system("xclip -i -selection clipboard", expand("%:p"))<CR>

#7


Here is my solution:

这是我的解决方案:

" 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>
" }}}

#1


TL;DR

:let @" = expand("%")>

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

这会将文件名复制到unamed寄存器,然后你可以使用旧的p来粘贴它。当然,您可以将其映射到一个键,以便更快地使用。

:nmap cp :let @" = expand("%")<cr>

:nmap cp:let @“= expand(”%“)

you can also use this for full path

你也可以用它来完整的路径

:let @" = expand("%:p")

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

Vim使用未命名的寄存器来存储已删除或复制(抽出)的文本,同样在粘贴它时会读取此寄存器中的文本。

Using let we can manually store text in the register using :let @" = "text" but we can also store the result of an expression.

使用let我们可以使用:let @“=”text“手动将文本存储在寄存器中,但我们也可以存储表达式的结果。

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.

在上面的例子中,我们使用扩展通配符和关键字的函数expand。在我们的示例中,我们使用expand('%')来扩展当前文件名。我们可以将其修改为扩展('%:p')以获取完整的文件名。

See :help let :help expand :help registers for details

请参阅:help let:help expand:help寄存器以获取详细信息

#2


Almost what you're asking for, and it might do: Ctrl+R % pulls the current filename into where you are (command prompt, edit buffer, ...). See this Vim Tip for more.

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

#3


If you want to put the current buffer filename in your system-level clipboard, try changing the register to @+:

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

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

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

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

Edit 20140421: I commonly use these, so I created some shortcuts. Linux Vims apparently operate slightly differently than Mac Vims, so there is a special case for that as well. If you put the following in your ~/.vimrc:

编辑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

Then for example <leader>cf will copy the relative path of the current buffer (the default leader is backslash (\)). I often use these for running commands on a file or doing other things on the command line. I don't really use the last filename / directory name often.

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

You might consider more intuitive mappings like <leader>cfr for relative, <leader>cfa for absolute, <leader>cff for just filename, <leader>cfd for directory.

您可以考虑更直观的映射,例如 cfr表示相对, cfa表示绝对, cff表示文件名, cfd表示目录。

#4


If you do :reg you will see the name of the current file in the % register. You can paste it with "%p, for example.

如果您执行:reg,您将在%寄存器中看到当前文件的名称。例如,您可以将其粘贴为“%p”。

If, like me, you often switch to the 'alternate' buffer, it is very handy that its full path-and-file-name are put in the # register. You can paste it with "#p, for example.

如果像我一样,你经常切换到'alternate'缓冲区,那么它的完整路径和文件名放在#register中是非常方便的。例如,您可以使用“#p粘贴它。

Note (just in case this is behaviour specific to my setup): I am using VIM 7.4.52 on Ubuntu 14.04.4 LTS.

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

#5


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

结合其他几个答案的信息:如果你想将当前的完整路径拉到一个文件并把它放到另一个窗口的命令缓冲区中,首先要做:let @“= expand(”%:p“),然后移动到另一个窗口并键入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

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

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

    键入:let @“= expand(”%:p“)(路径被拉到主剪贴板缓冲区。)

  2. Open a new window with :sp

    用:sp打开一个新窗口

  3. Type :e Ctrl+R"

    键入:e Ctrl + R“

  4. Use the arrow keys to go back to Circle and change it to Square, and press <CR>

    使用箭头键返回Circle并将其更改为Square,然后按

End: Editing src/com/benatkin/paint/shapes/Square.java

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

#6


I use xclip to access X's clipboard, so I use:

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

nmap <localleader>d :call system("xclip -i -selection clipboard", expand("%:p"))<CR>

#7


Here is my solution:

这是我的解决方案:

" 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>
" }}}