tags:

views:

521

answers:

2

: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?

A: 

You can do this:

:bufdo vimgrep /pattern/ %

% substitutes the buffer name.

Jerub
+3  A: 

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 BufffersList()
  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(),' ')
Luc Hermitte
BuffersList works exactly as I wanted. Thanks Luc and Zathrus.
iii
This answer was helpful, but <a="http://www.vim.org/scripts/script.php?script_id=311">grep.vim</a> is a vim plugin that uses all variants of the system's grep programs, searches for the word under the cursor (by default), and can search files, args and open buffers.
iii