$ 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?


  • 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

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.

| 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.