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
-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-dir=.git which reduces my pain considerably!
Another approach could be to combine a dotfile with files and directories (with a trailing
and parse them with a small script when you set your
GREP_OPTIONS="-In $(perl -pe '/\/$/?(s/^/--exclude-dir=/,s/\/$//):s/^/--exclude=/' < ~/.grep_excludes | xargs)";
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: