List Vim active buffers with Selecta

Migrating from Command-T to Selecta left one thing missing, list the active buffers. In order to feed Selecta through STDIN I had to create a function that outputs a shell command with all active buffers line by line. Here's the function:

function! ListActiveBuffers()
  let bufferlist = []
  for l:bni in range(bufnr("$"), 1, -1)
    if buflisted(l:bni)
      call add(bufferlist, bufname(l:bni))
  return "echo ".join(bufferlist, ' ')." | tr ' ' '\\n' "

This function goes through all buffer numbers, check if the buffer is listed and adds to a list called bufferlist. Then it concatenates all elements outputting a shell command, as an example:

echo buffer1 buffer2 buffer3 | tr ' ' '\n'

Then this is "pipped" (fed into SelectaCommand()) to Selecta. Now we create a Vim mapping:

nnoremap <leader>b :exec ":e " SelectaCommand(ListActiveBuffers())<cr>

And we are done.

Published in Jan 10, 2014