meder omuraliev
meder omuraliev

Reputation: 186692

How can I automatically open a file if it's the only result in grep?

[meder@kat directoryName]$ grep -RlI "send you instr" *
application/views/scripts/auth/forgot.phtml
[meder@kat directoryName]$

Is there a quick hack to referencing the only result? Can I somehow pipe it to vim?

Upvotes: 3

Views: 354

Answers (3)

wose
wose

Reputation: 456

vim `grep -RlI "send you instr" *`

Upvotes: 1

Brian Campbell
Brian Campbell

Reputation: 333136

If you're OK with it opening all results in Vim, you could just do:

vim $(grep -RlI "send you instr" *)

You'll be put into a buffer with the first matching file, and can navigate to the others with :next.

Upvotes: 4

eumiro
eumiro

Reputation: 213045

Open a new file in vim and insert the found filename as a text:

grep -RlI "send you instr" * | vim -

Open the found file directly in vim:

grep -RlI "send you instr" * | xargs vim

Upvotes: 3

Related Questions