I'm on linux and I'm querying the list of files open in a gvim server so I can pull it up with

gvim --servername <name> --remote <file>

To do so, I wrote a helper function that returns buffers

function! returnbufferlist()
    execute "silent redir @m"
    execute "silent buffers"
    execute "silent redir END"
    "execute "<ESC><ESC>"
    let a=@m
    return a

My external script loops through all gvim servers, and runs

gvim --remote-expr 'returnbufferlist()'

I check my filename against the list of buffers, and if it's open in the server, I run

gvim --servername <name> --remote <file>

as above. Is there a more efficient way of doing this? Is there a dbus interface that allows me to do the same thing more efficiently? Now I'm doing some regex matching that's trying to match my filename with the bufferlist, and this doesn't always work as intended since buffers are not listed with full paths.

1 Answer 1


You could use your own code to list the buffers, instead of execute "silent buffers":

for i in range(1, bufnr('$'))
  if buflisted(i)
    echo i . ' ' . fnamemodify(bufname(i), ':p')

Here fnamemodify() with ':p' will give you the full path to each filename.

The check for &buflisted will avoid listing buffers which have been closed, help buffers, and other buffers which have unlisted themselves, such as the MiniBufExplorer plugin.

  • That's great, but I think you meant endif instead of fi. Oct 2, 2016 at 22:45
  • As an aside, buflisted(i) and getbufvar(i, '&buflisted') are equivalent but the former is shorter (:help buflisted() for details). On the other hand, fnameescape() could also be useful when filenames contain special characters. Oct 2, 2016 at 23:15
  • Thanks for the {correc,sugges}tions @JairLópez. I have incorporated some of them. Whether to use fnameescape() really depends on how he handles the filenames next. (It might be easier without it, but you are right that really it is safer to use it, e.g. for those pesky filenames containing newlines!) Oct 3, 2016 at 2:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.