how to call vim on files that are the results of a grep -l command in bash?

50 views
Skip to first unread message

DwigtArmyOfChampions

unread,
May 24, 2019, 7:40:55 AM5/24/19
to vim_use
From a bash shell I can type "grep -l 'foo' *" and that will output a list of files that contain 'foo'. I want to vim that list of files. In other words, assuming the grep command returns file1, file2, ... filen, I want to run the command:

vim file1 file2 file3 ... filen

I tried "grep -l 'foo' * | vim" and "grep -l 'foo' * | xargs | vim' but those didn't work. Any ideas?

Reid Thompson

unread,
May 24, 2019, 7:52:41 AM5/24/19
to vim...@googlegroups.com, Reid Thompson
On Fri, 2019-05-24 at 04:40 -0700, DwigtArmyOfChampions wrote:
> [EXTERNAL SOURCE]
> --


vim $(grep -l foo)


Reid Thompson

unread,
May 24, 2019, 7:52:52 AM5/24/19
to vim...@googlegroups.com, Reid Thompson
this is the xargs one


ls ycm* | xargs vim

Kit

unread,
May 24, 2019, 8:22:38 AM5/24/19
to vim...@googlegroups.com
grep -l 'foo' * | xargs vim
--
Kit

2019-05-24 13:40 GMT+02:00, DwigtArmyOfChampions > I tried "grep -l

aro...@vex.net

unread,
May 24, 2019, 9:10:02 AM5/24/19
to vim...@googlegroups.com
> grep -l 'foo' * | xargs vim
> --

Or, if the number isn't likely huge:

vim $(grep -l 'foo' *)

Tim Chase

unread,
May 24, 2019, 9:32:10 AM5/24/19
to DwigtArmyOfChampions, vim...@googlegroups.com
On 2019-05-24 04:40, DwigtArmyOfChampions wrote:
> From a bash shell I can type "grep -l 'foo' *" and that will output
> a list of files that contain 'foo'. I want to vim that list of
> files. In other words, assuming the grep command returns file1,
> file2, ... filen, I want to run the command:
>
> vim file1 file2 file3 ... filen

As others have mentioned, you have

vim $(grep -l 'foo' *)

and

grep -l foo * | xargs vim

(which then has vim complain that input isn't from stdin)

Moreover, vim has some built-in grepping functionality which might be
helpful:

:vimgrep foo *

then you can use

:cn
:cN
:crew

to navigate them, as well as (at least as of a somewhat recent
version of vim) the

:cdo
:cfdo

commands which let you perform operations across all the matches or
across all the matching files. Especially since :vimgrep supports all
the power of Vim's regular expressions which can do things that
grep(1) can't do.

-tim





Gary Johnson

unread,
May 24, 2019, 11:22:06 AM5/24/19
to vim_use
Using xargs with an interactive program like vim has problems, such
as leaving your terminal in an odd state after vim exits. A better
solution is to use GNU parallel:

$ grep -l 'foo' * | parallel --tty -X vim

Regards,
Gary

Tony Mechelynck

unread,
May 24, 2019, 11:41:44 AM5/24/19
to vim_use
Another possibility is to tell Vim that the file to be read will be
found on stdin:

grep -l 'foo' | view -

One problem with this approach is that using "view" (because stdin is
essentially non-writable) will make ":new foobar" open the new file
read-only as if you had used :sview rather than :new. IIUC in order to
make the new file writable you will have to use ":setl noro" after
making it current.

Best regards,
Tony.

Paul

unread,
May 26, 2019, 7:03:38 AM5/26/19
to vim_use
I use the quickfix list for this. Previous commands will break on whitespace in filepaths. This won't (but it will on newlines - I didn't figure out how to work it with Vim for those), but you'll have to tell your command to use a null as a separator, eg. `grep -Zl foo * | pvim`:

pvim() {
local files=()
while IFS= read -rd '' file; do
files+=("$file")
done
(( ${#files[@]} )) && vim -q <( printf '%s:1: \n' "${files[@]}" ) < /dev/tty
}
signature.asc
Reply all
Reply to author
Forward
0 new messages