Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 606
Aaron Avatar asked Nov 07 '08 06:11

Aaron


4 Answers

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(),' ')
like image 158
Luc Hermitte Avatar answered Nov 15 '22 23:11

Luc Hermitte


You can do this:

:bufdo vimgrep /pattern/ %

% substitutes the buffer name.

like image 29
Jerub Avatar answered Nov 16 '22 00:11

Jerub


To [vim]grep the list of files in the argument list, you may use ## (see :help cmdline-special).

:vimgrep /re/ ##

I am unaware of a similar shorthand for the buffer list, but you may be able to do something like:

:argdelete ##
:bufdo argadd %

... and then use ##. Or use :n to open new files (which will be added to the arg list) instead of :e.

like image 30
Steve Avatar answered Nov 16 '22 00:11

Steve


Here is a slightly refined version of one of the answers. The following command searches for the pattern in all opened tabs and remembers results in quickfix list:

:cex [] | tabdo vimgrepa /pattern/ %

cex [] sets contents of quickfix list to empty list. You need to call it first because vimgrepa accumulates search results from all the tabs. Also, you can replace tabdo with argdo, bufdo and windo.

To view search results execute:

:cope

This method, however, has limitation: it can only search in tabs which already have file names assigned to them (% would not expand in a new tab).

EDIT: You can also shortcut the command into function in your ~/.vimrc like this:

function TS(text)
    exe "cex [] | tabdo vimgrepa /" . a:text . "/ %"
endfunction
command -nargs=1 TS call TS(<q-args>)
cnoreabbrev ts TS

With last line you can call your function like this:

:ts from game import

where words after ts is a search pattern. Without last line you have to type function name in upper case.

like image 3
baltazar Avatar answered Nov 16 '22 00:11

baltazar