Grep and Node.js

One of my most frequently used command line utilities is Grep, specifically grep -lir which searches for text inside files (great for finding where else I referenced that variable I just renamed).

As I started using Node.js more and more (and consequently my node_modules folder grew bigger and bigger) I found myself getting more annoyed by all of the “false positives” that grep found. Luckily grep version 2.5.2 introduced the --exclude_dir which does exactly what you think it would. So then I started using
grep -lir --exclude_dir=node_modules for all of my grepping needs.

Alas, because I’m a #LazyWeb programmer this became tedious and annoying to me too. I figured there had to be an environmental variable I could set in zsh so I set to Googling. I found that there is indeed a GREP_OPTIONS variable that you can set but it is not recommended. So I did as the man page suggested and created a small script named grep in my ~/bin directory (making sure that the first line in my .zshrc was export PATH=$HOME/bin:/usr/local/bin:$PATH).

Here’s the contents of my tiny grep:

! /bin/sh
export PATH=/usr/bin
exec grep --exclude-dir=node_modules "$@"

…and now all I have to do is:
grep -lir "where_did_i_put_that_variable" *

N.B. I didn’t add the -lir to my script because while I use grep for searching in files 90% of the time, the other 10% I do use it to search for file names and I’d rather type -lir than /usr/bin/grep

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.