如何列出 Vim 中打开的所有缓冲区的文件路径?

发布于 2024-12-02 00:40:08 字数 475 浏览 1 评论 0原文

有没有办法列出 Vim 中所有打开的缓冲区?我想查看每个打开的缓冲区的完整文件路径并将列表保存到外部文件,或者将其粘贴到另一个文本文档中。

解决方案

这是一场非常艰苦的比赛!下面的所有三个建议都效果很好。我选择了 Luc Hermitte's 并将其添加到我的 .vimrc 文件中:

noremap <silent> <leader>so :call writefile( map(filter(range(0,bufnr('$')), 'buflisted(v:val)'), 'fnamemodify(bufname(v:val), ":p")'), 'open_buffers.txt' )<CR>

所以现在输入 ,so 会将所有打开的缓冲区的完整路径保存到当前目录中open_buffers.txt 文件。

Is there a way to list all open buffers in Vim? I’d like to view the full file path to every open buffer and save the list to an external file, or yank it for pasting into another text document.

Solution

This was a very hard contest! All three of the suggestions below worked well. I went with Luc Hermitte’s and added this to my .vimrc file:

noremap <silent> <leader>so :call writefile( map(filter(range(0,bufnr('

So now typing ,so will save all the full path of all open buffers to the current directory in the open_buffers.txt file.

)), 'buflisted(v:val)'), 'fnamemodify(bufname(v:val), ":p")'), 'open_buffers.txt' )<CR>

So now typing ,so will save all the full path of all open buffers to the current directory in the open_buffers.txt file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

抚你发端 2024-12-09 00:40:08

我会使用“简单”:

echo map(filter(range(0,bufnr('

With:

  • range(0,bufnr('$')) 来获得 |List|所有可能的缓冲区编号
  • filter(possible_buffers, 'buflisted(v:val)') 将列表限制为实际列出的缓冲区 - 您可能更喜欢 bufexist() 它还会显示帮助缓冲区等。
  • map(listed_buffer, 'nr_to_fullpath(v:val)') 将所有缓冲区编号转换为完整路径名
  • bufname() 将单个缓冲区编号转换为(简化的)路径名
  • fnamemodify(pathname, ':p') 从相对路径名获得完整的绝对路径名。

:echo 更改为 call writefile(pathname_list, 'filename'),仅此而已,或者更改为 :put= 等。

)), 'buflisted(v:val)'), 'fnamemodify(bufname(v:val), ":p")')

With:

  • range(0,bufnr('$')) 来获得 |List|所有可能的缓冲区编号
  • filter(possible_buffers, 'buflisted(v:val)') 将列表限制为实际列出的缓冲区 - 您可能更喜欢 bufexist() 它还会显示帮助缓冲区等。
  • map(listed_buffer, 'nr_to_fullpath(v:val)') 将所有缓冲区编号转换为完整路径名
  • bufname() 将单个缓冲区编号转换为(简化的)路径名
  • fnamemodify(pathname, ':p') 从相对路径名获得完整的绝对路径名。

:echo 更改为 call writefile(pathname_list, 'filename'),仅此而已,或者更改为 :put= 等。

I'd have use the "simple":

echo map(filter(range(0,bufnr('

With:

  • range(0,bufnr('$')) to have a |List| of all possible buffer numbers
  • filter(possible_buffers, 'buflisted(v:val)') to restrict the list to the buffers that are actually listed -- you may prefer bufexist() that'll also show the help buffers, etc.
  • map(listed_buffer, 'nr_to_fullpath(v:val)') to transform all the buffer numbers into full pathnames
  • bufname() to transform a single buffer number into a (simplified) pathname
  • fnamemodify(pathname, ':p') to have a full absolute pathname from a relative pathname.

Change :echo to call writefile(pathname_list, 'filename'), and that's all, or to :put=, etc.

)), 'buflisted(v:val)'), 'fnamemodify(bufname(v:val), ":p")')

With:

  • range(0,bufnr('$')) to have a |List| of all possible buffer numbers
  • filter(possible_buffers, 'buflisted(v:val)') to restrict the list to the buffers that are actually listed -- you may prefer bufexist() that'll also show the help buffers, etc.
  • map(listed_buffer, 'nr_to_fullpath(v:val)') to transform all the buffer numbers into full pathnames
  • bufname() to transform a single buffer number into a (simplified) pathname
  • fnamemodify(pathname, ':p') to have a full absolute pathname from a relative pathname.

Change :echo to call writefile(pathname_list, 'filename'), and that's all, or to :put=, etc.

花想c 2024-12-09 00:40:08

要列出缓冲区的绝对路径,您可以使用:

:!echo %:p

如果将其包装到记录中,您将得到您需要的内容,例如:

qq
:!echo %:p >> my_buffers
:bnext
q

现在,当您有缓冲区时执行宏次数,例如:

10@q

您将在文件 my_buffers

不过可能是更好的方法:-)

To list the absolute path for a buffer you can use:

:!echo %:p

If you wrap that into a recording you will get what you need, e.g.:

qq
:!echo %:p >> my_buffers
:bnext
q

Now execute the macro number of times as you have buffers, e.g.:

10@q

and you will have the result in the file my_buffers

Probably a better way though :-)

屋顶上的小猫咪 2024-12-09 00:40:08

这应该有效:

:redi @"|ls|redi END
:new +pu
:%s/[^"]*"\([^"]*\)".*/\=fnamemodify(submatch(1), ":p")/e
:g/^$/d

解释:

  • :redi 将重定向消息
  • :redi @" 将把消息重定向到 @" 又名未命名寄存器
  • :redi END 停止重定向
  • :ls 将打印出所有非隐藏缓冲区
  • :new 在分割中创建缓冲区
  • :new +{cmd }+cmd 将为新缓冲区执行命令。
  • :new +pu 在新的缓冲区正则表达式上执行 :pu 或 put 命令
  • 基本上匹配整行并捕获引号之间的内容
  • \=:s/ 的替换部分中将执行表达式
  • fnamemodify(submatch(1), ":p") 将扩展捕获的数据,即 submatch( 1)
  • :g/^$/d 删除所有空行

更多信息:

:h /\=
:h :g
:h :new
:h :pu
:h :redi
:h :ls
:h fnamemodify()
:h :d

This should work:

:redi @"|ls|redi END
:new +pu
:%s/[^"]*"\([^"]*\)".*/\=fnamemodify(submatch(1), ":p")/e
:g/^$/d

Explanation:

  • :redi will redirect the messages
  • :redi @" will redirect the message to @" aka the unnamed register
  • :redi END stops redirection
  • :ls will print out all non-hidden buffers
  • :new create a buffer in a split
  • :new +{cmd} the +cmd will execute a command for the new buffer.
  • :new +pu execute the :pu or put command on the new buffer
  • regex basically matches the entire line and captures the content between the quotes
  • \= in the replacement part of :s/ will execute an expression
  • fnamemodify(submatch(1), ":p") will expand the captured data aka submatch(1)
  • :g/^$/d delete all blank lines

More information:

:h /\=
:h :g
:h :new
:h :pu
:h :redi
:h :ls
:h fnamemodify()
:h :d
做个少女永远怀春 2024-12-09 00:40:08

bufexplorer 脚本显示所有打开缓冲区的路径,但它也显示其他信息,因此不适合复制并粘贴到另一个文档中。这是屏幕截图

The bufexplorer script shows the path of all open buffers, however it also shows other information so it is not ideal for yanking and pasting into another document. Here's a screenshot

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文