开发者

How to list the file paths of all buffers open in Vim?

开发者 https://www.devze.com 2023-04-01 03:04 出处:网络
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.

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 m开发者_如何学Goy .vimrc file:

noremap <silent> <leader>so :call writefile( map(filter(range(0,bufnr('$')), '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.


I'd have use the "simple":

echo map(filter(range(0,bufnr('$')), '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.


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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号