Search code examples
vim

Variable that holds "list of all open buffers" in Vim?


:vimgrep looks like a really useful thing.

Here's how to use it:

:vim[grep][!] /{pattern}/[g][j] {file} ... 

:help says that you can essentially glob {file} to name, say, *.c for the current directory. I may have started Vim with a list of files that is complicated enough that I don't want to manually type it in for {file}, and besides Vim already knows what those files are.

What I would like to do is vimgrep over any of:

  • :args
  • :files
  • :buffers

What variable(s) would I use in place of {file} to name, respectively, any of those lists in a vimgrep command?


Solution

  • Can't you catch the result in these commands into a register (:h :redir), and insert it back into :vimgrep call (with a :exe).

    Something like:

    :exe "vimgrep/pattern/ " . lh#askvim#Exe(':args')
    

    Notes:

    • lh#askvim#Exe is just a wrapper around :redir ; nothing really complex
    • some of these results may need some processing (see :args that adds square brackets)
    • Sometimes there is a function that returns exactly what you are looking for, see join(argv(), ' ') in :args case
    • Regarding :buffers, may be something like:

    .

    function BuffersList()
      let all = range(0, bufnr('$'))
      let res = []
      for b in all
        if buflisted(b)
          call add(res, bufname(b))
        endif
      endfor
      return res
    endfunction
    :exe 'vimgrep/pattern/ '.join(BuffersList(),' ')