Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 636
Jonathan Beebe Avatar asked Aug 29 '11 21:08

Jonathan Beebe


People also ask

How do I navigate between buffers in Vim?

Pressing Alt-F12 opens a window listing the buffers, and you can press Enter on a buffer name to go to that buffer. Or, press F12 (next) or Shift-F12 (previous) to cycle through the buffers.

How do I close all buffers in Vim?

I use a variant of your solution: :%bd<CR><C-O>:bd#<CR> This will delete all buffers, then use <C-O> to get restore the position in the current file, then :bd# to remove the unamed buffer. This closes all buffers and leaves you in the same location in the file.

How does the vi editor use buffers?

Whenever you delete something from a file, vi keeps a copy in a temporary file called the general buffer. You can also delete or copy lines into temporary files called named buffers that will let you reuse those lines during your current vi work session.


1 Answers

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.

like image 161
Luc Hermitte Avatar answered Nov 15 '22 10:11

Luc Hermitte