1 __GREP_CACHE_FILE="$ZSH_CACHE_DIR"/grep-alias
3 # See if there's a cache file modified in the last day
4 __GREP_ALIAS_CACHES=("$__GREP_CACHE_FILE"(Nm-1))
5 if [[ -n "$__GREP_ALIAS_CACHES" ]]; then
6 source "$__GREP_CACHE_FILE"
8 grep-flags-available() {
9 command grep "$@" "" &>/dev/null <<< ""
12 # Ignore these folders (if the necessary grep flags are available)
13 EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
15 # Check for --exclude-dir, otherwise check for --exclude. If --exclude
16 # isn't available, --color won't be either (they were released at the same
17 # time (v2.5): https://git.savannah.gnu.org/cgit/grep.git/tree/NEWS?id=1236f007
18 if grep-flags-available --color=auto --exclude-dir=.cvs; then
19 GREP_OPTIONS="--color=auto --exclude-dir=$EXC_FOLDERS"
20 elif grep-flags-available --color=auto --exclude=.cvs; then
21 GREP_OPTIONS="--color=auto --exclude=$EXC_FOLDERS"
24 if [[ -n "$GREP_OPTIONS" ]]; then
25 # export grep, egrep and fgrep settings
26 alias grep="grep $GREP_OPTIONS"
27 alias egrep="grep -E $GREP_OPTIONS"
28 alias fgrep="grep -F $GREP_OPTIONS"
30 # write to cache file if cache directory is writable
31 if [[ -w "$ZSH_CACHE_DIR" ]]; then
32 alias -L grep egrep fgrep >| "$__GREP_CACHE_FILE"
37 unset GREP_OPTIONS EXC_FOLDERS
38 unfunction grep-flags-available
41 unset __GREP_CACHE_FILE __GREP_ALIAS_CACHES