Search code examples
filevimenumerate

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


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('$')), '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.


Solution

  • 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.