如何列出 Vim 中打开的所有缓冲区的文件路径?
有没有办法列出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用“简单”:
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":
With:
range(0,bufnr('$'))
to have a |List| of all possible buffer numbersfilter(possible_buffers, 'buflisted(v:val)')
to restrict the list to the buffers that are actually listed -- you may preferbufexist()
that'll also show the help buffers, etc.map(listed_buffer, 'nr_to_fullpath(v:val)')
to transform all the buffer numbers into full pathnamesbufname()
to transform a single buffer number into a (simplified) pathnamefnamemodify(pathname, ':p')
to have a full absolute pathname from a relative pathname.Change
:echo
tocall writefile(pathname_list, 'filename')
, and that's all, or to:put=
, etc.要列出缓冲区的绝对路径,您可以使用:
如果将其包装到记录中,您将得到您需要的内容,例如:
现在,当您有缓冲区时执行宏次数,例如:
您将在文件
my_buffers
不过可能是更好的方法:-)
To list the absolute path for a buffer you can use:
If you wrap that into a recording you will get what you need, e.g.:
Now execute the macro number of times as you have buffers, e.g.:
and you will have the result in the file
my_buffers
Probably a better way though :-)
这应该有效:
解释:
:redi
将重定向消息:redi @"
将把消息重定向到@"
又名未命名寄存器:redi END
停止重定向:ls
将打印出所有非隐藏缓冲区:new
在分割中创建缓冲区:new +{cmd }
的+cmd
将为新缓冲区执行命令。:new +pu
在新的缓冲区正则表达式上执行:pu
或 put 命令\=
在:s/
的替换部分中将执行表达式fnamemodify(submatch(1), ":p")
将扩展捕获的数据,即submatch( 1)
:g/^$/d
删除所有空行更多信息:
This should work:
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\=
in the replacement part of:s/
will execute an expressionfnamemodify(submatch(1), ":p")
will expand the captured data akasubmatch(1)
:g/^$/d
delete all blank linesMore information:
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