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

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

Tags:

Vim