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

I work in python, so 95% or more of the time my grep search is

grep "thing to find" *.py

or something similar. I'm working in Windows 7, but being a unix guy, I am lazy with my keystrokes, so I would like to set grep to automatically search *.py unless I specify something different.

Is there anything I can add to GREP_OPTIONS to do this? Or something similar?

share|improve this question

migrated from unix.stackexchange.com Feb 15 '14 at 19:56

This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.

    
@slyx that should be an answer, but with "$@" instead of $1. – jordanm Feb 10 '14 at 14:49
    
@jordanm Thanks, I noticed the difference! I've just posted it as an answer ! – Slyx Feb 10 '14 at 14:50
    
Ah, left out a little detail. I'm working on a Win7 box most of the time. So no functions. I can put it in a batch file {grep %1 *.py} which is better than nothing. I just was hoping for a more canonical/config file or environment variable type solution. – Skip Huffman Feb 10 '14 at 16:12
1  
@SkipHuffman, In windows envirenement (MSDOS) you can also define a DOSKEY macro : DOSKEY pygrep=grep $* *.py and call it the same way you see in the answers. unfortunately DOSKEY and BATCH parameters number is limited to 9 in Windows environment so don't forget to use pygrep "Your search between double quote limiters" – Slyx Feb 11 '14 at 1:55

You can define a function pygrep which will search inside your py files only :

pygrep() { 
    grep "$@" *.py
}
share|improve this answer
    
@jordanm Thanks for the POSIX compliant edit ! – Slyx Feb 11 '14 at 7:05

Create a function

mygrep(){ grep "$@" *.py}

Run it

mygrep main

Add the function to your login startup file.

share|improve this answer
1  
What's the point of the alias? Why not just call the function mygrep and be done with it? – terdon Feb 10 '14 at 15:11
    
My function names tend to be long but my aliases short. I did say it was optional. – X Tian Feb 10 '14 at 15:13
    
Yes you did, I was just wondering why you would want the alias at all, it seems to simply add a layer of complexity without doing anything useful. Am I missing something? – terdon Feb 10 '14 at 15:14
    
That's probably because I started defining aliases and then functions came along, adding that layer of complexity. So it's a bit of an old habit. – X Tian Feb 10 '14 at 15:21
    
Ah, yes, I know all about old habits :). So aliases predate functions? I had no idea. – terdon Feb 10 '14 at 15:22
up vote 0 down vote accepted

I ended up creating a mini batch file:

grep -n "%*" *.py

saves a few keystrokes, and matches my normal brain cycle. "Where is this phrase in my python code?"

Thanks everyone for the input!

share|improve this answer

You must log in to answer this question.

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