grep: defaults for web developers

When greping over a codebase, there can be so many different things to trip you up and flag false positives, or files that you definitely didn’t want to grep through (I’m looking at you minified JavaScript and CSS!). I thought I’d share some useful items I have as my GREP_OPTIONS.

-I Ignore binary files. Makes sense, not need to check then in most instances…

-n Include line numbers. Self-explanatory!

--exclude/--exclude-dir Skip certain files/directories. You can specify these multiple times to exclude different items as needed. I have --exclude=min.{js,css}, --exclude-dir=.svn and --exclude-dir=.git which reduces my pain considerably!

Another approach could be to combine a dotfile with files and directories (with a trailing /) listed:


and parse them with a small script when you set your GREP_OPTIONS:

# deprecated
GREP_OPTIONS="-In $(perl -pe '/\/$/?(s/^/--exclude-dir=/,s/\/$//):s/^/--exclude=/' < ~/.grep_excludes | xargs)";

# preferred
alias grep="grep -In $(perl -pe '/\/$/?(s/^/--exclude-dir=/,s/\/$//):s/^/--exclude=/' < ~/.grep_excludes | xargs)";

that way as you encounter new files you want to exclude, they can be added easily, without touching your base dotfiles.


I use a 256-colour terminal emulator combined with tmux most of the time and find that changing the colours for grep results can make your life a lot easier to spot exactly what it is you're searching for in context. Setting the environment variable GREP_COLORS allows you to specify ANSI codes to select the foreground and background colours via identifiers. I've made a tool for configuring and customising GREP_COLORS which is available here.

My current set up is loosely based on the Monokai color scheme:

which is set by the following:


Leave a Reply

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.