pev.hall
pev.hall

Reputation: 507

VIM: grep search all files listed in text file

I have a complected project hierarchy for my FPGA projects. I have written a passer which examines the "Vivado/ISE" project file and returns a file containing a list of all the source file the project users (I then run ctags over this list).

I would like to be able to search this list of files from vim 7.4 without needing to do a whole recursive search through the FPGA library. I can do this from the linux command line using something like:

cat ./files.txt | xargs grep -Hn my_function

Where ./files.txt contains the list of files I would to search over. However I would like to be able to use vim's internal grep function. I would also like to be able to run this from both Windows and Linux.

Is there an easy way to pass a list of files (contained within a file) to vim's grep ex function?

Upvotes: 1

Views: 982

Answers (3)

Mischa
Mischa

Reputation: 2298

If you have opened all files in Vim—eg with vim `<files.txt`—then you can search all of them with (e.g.)

:bufdo g/my_function/

If you :set nu you'll get line numbers as per grep -nHth

Upvotes: 1

romainl
romainl

Reputation: 196566

First step, populate the argument list with all the files in your list:

" in UNIX-like environments
:args `cat files.txt`

" in Windows
:args `type files.txt`

Second step, search for pattern in the argument list:

:vim pattern ##

Upvotes: 2

Sato Katsura
Sato Katsura

Reputation: 3086

You should probably use a plugin such as CtrlSF. But if you insist to do it with Vim alone, you can do something like this:

function! Grep(what, where)
    exec join(extend(['vimgrep', a:what],
        \ map(filter(readfile(a:where), 'v:val !=# "" && filereadable(v:val)'),
        \ 'fnameescape(v:val)')))
    copen
endfunction

command! -nargs=+ Grep call Grep(<f-args>)

Then you'd just call :Grep /pattern/ filelist, instead of :vimgrep /pattern/ ....

Upvotes: 2

Related Questions