Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
$ grep -l -R "myEditor_Init" *

For example, I would like to grep and vim the third file with one command, how could I do this?


share|improve this question
What makes the third file more interesting than the other two? – Ignacio Vazquez-Abrams Feb 18 '12 at 3:28
becuase its the function 'myEditor_init' which I want to edit is in it. – Jichao Feb 18 '12 at 3:48
Nothing in your grep command indicates that. – Ignacio Vazquez-Abrams Feb 18 '12 at 3:52
up vote 3 down vote accepted

As Ignacio implies, you can re-work your grep command to obtain only the one file you want, for instance:

$ grep -l -R --include="*.js" "myEditor_Init" *

In a case like this, you can edit it with, e.g. (use backticks to supply the result as an argument to vim:

$ vim `grep -l -R --include="*.js" "myEditor_Init" *`

If you really want to use * instead of limiting to *.js files, you could still do:

$ vim `grep -l -R "myEditor_Init" * | tail -1`

I often find myself (using csh or bash) doing something like the following:

$ grep -l -R --include="*.js" "myEditor_Init" *
$ vim `!!`

where !! repeats the previous command, and the backticks feed the result of that command as an argument to vim.

share|improve this answer
Just edited the suggested commands above, to allow for recursing in all local subdirectories. – Lars Rohrbach Feb 18 '12 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .