Skip to content

Instantly share code, notes, and snippets.

@peregrinogris
Last active December 2, 2021 20:17
Show Gist options
  • Save peregrinogris/1256258 to your computer and use it in GitHub Desktop.
Save peregrinogris/1256258 to your computer and use it in GitHub Desktop.
Custom flavored grep
#!/bin/bash
EXCLUDEDIR=${EXCLUDEDIR:-"env/*"}
COLOR=${COLOR:-always}
CONTEXT=${CONTEXT:-0}
grep -IiRn --exclude="$EXCLUDE" --exclude-dir="$EXCLUDEDIR" --color=$COLOR -C$CONTEXT "$1" * \
| less -iFRX
#!/bin/bash
if [[ "$EXCLUDE" != "" ]]; then
EXCLUDE="!$EXCLUDE"
fi
COLOR=${COLOR:-always}
CONTEXT=${CONTEXT:-0}
rg -in -g "$EXCLUDE" --color=$COLOR -C$CONTEXT "$1" \
| less -iFRX
@peregrinogris
Copy link
Author

Custom Flavored Grep

Some time ago I realized that I used grep too often with an irksome set of flags, so I made this bash script to set them for me. I mainly use grepc to browse through the mozilla-central source code, to find function definitions, calls, etc.
I know you can probably do this from within vim or emacs, but sometimes you're wandering around in the terminal and this just does the trick

Installation

I generally save this file as /usr/local/bin/grepc, setting it as executable.

Usage

Run:

$ grepc <search_string>

And the output will be a list of the files with that string, showing the matched line with the string highlighted and line-number.

Options:

Use the variable COLOR to override the default color output (--color=always):

$ COLOR=never grepc <search_string>

Use the variable EXCLUDEDIR to override the default excluded folder (env/*):

$ EXCLUDEDIR=tests/* grepc <search_string>

Use the variable EXCLUDE to exclude files from the results:

$ EXCLUDE=test_*.py grepc <search_string>

Use the variable CONTEXT to add that many context lines before and after the match:

$ CONTEXT=3 grepc <search_string>

Changelog

  • 2015-01-05: add context variable
  • 2014-08-17: removed legacy svn stuff, added overrides for exclude folders and files
  • 2014-05-27: added support to override colored output
  • 2011-11-16: added pagination for when the results are larger than the page size. Some extra indentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment