Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.
$ grep -l -R "myEditor_Init" *
Public/Js/eWebEditor/dialog/ewebeditor.htm
Public/Js/eWebEditor/ewebeditor.htm
Public/Js/eWebEditor/js/ewebeditor.js

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

Thanks.

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

1 Answer 1

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" *
Public/Js/eWebEditor/js/ewebeditor.js

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" *
Public/Js/eWebEditor/js/ewebeditor.js
$ 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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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