1

I want to have the following behavior with grep:

grep --java <rest of the command>

Where --java would either dynamically convert to --include=*.java or internally be equivalent to it. Is there a way to do this in my .cshrc file? I tried using the complete command but I can't seem to have the replacement work on the current word.

  • This would typically be done with a shell function wrapper around grep, which might be tricky in a shell like tcsh, which only provides aliases. ack might be another option, as it has --type-set to specify filenames to search. – thrig Feb 8 '16 at 17:58
0

If you're open to using a different syntax (which is also shorter and easier to type in this case), just create an alias for it. Add this to your ~/.cshrc:

alias jgrep grep --include="*.java"

Then, you can run something like:

jgrep foo *

And it will run

grep --include="*java" foo *
  • But I would like the behavior to be dynamic, like the syntax in complete command. So a given word in a set of words I want to behave like this would equate to going from --<word> to --include=*<word>. – Cris RR Feb 8 '16 at 18:35
  • @CrisRR I don't understand what you mean. – terdon Feb 8 '16 at 18:37
  • Like a substitution rule, that says if grep followed by --{gradle | groovy | java} then -> --include=*.{gradle | groovy | java}, but only have this behaviour for the ones I declare in the list. – Cris RR Feb 8 '16 at 18:56
  • @CrisRR well, I would just have 3 aliases, once for each of gradle, groove and java. I don't see what you gain by having it dynamically expanded. – terdon Feb 8 '16 at 18:57
0

This is not something you can do in tcsh.

Your options are:

  • Use an alias, as already mentioned by terdon:

    alias jgrep grep --include="*.java"
    
  • Set up a completion pattern. For example:

    complete grep 'c/--/(complete)/' 'n/--complete/(*.java *.rb *.py)/' 
    

    This will allow you to type grep --<Tab><Tab>j<Tab>.

  • Use a variable:

    set java = '--include=*.java'  
    grep -r "$java" foo
    

    Personally, I wouldn't necessarily consider that to be easier.

Finally, I find that the_silver_searcher is a lot more useful and faster for "grepping" code trees. It's something you might want to check out.

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.