Reputation: 5226
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.
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('$')), '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.
Upvotes: 7
Views: 1445
Reputation: 32986
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 numbersfilter(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 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
to call writefile(pathname_list, 'filename')
, and that's all, or to :put=
, etc.
Upvotes: 10
Reputation: 45177
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\=
in the replacement part of :s/
will execute an expressionfnamemodify(submatch(1), ":p")
will expand the captured data aka submatch(1)
:g/^$/d
delete all blank linesMore information:
:h /\=
:h :g
:h :new
:h :pu
:h :redi
:h :ls
:h fnamemodify()
:h :d
Upvotes: 3
Reputation: 13536
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
Upvotes: 1
Reputation: 45670
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 :-)
Upvotes: 3