aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/grep.bash
blob: 624baf30498f4510b97062f75792e190c7a2d25f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Our ~/.profile should already have made a directory with the supported
# options for us
[[ -d $HOME/.cache/grep ]] || return

# Store whether we have colors in a variable
declare -i colors
colors=$( {
    tput Co || tput colors
} 2>/dev/null )

# Use GREPOPTS to add some useful options to grep(1) calls if applicable; we
# use a function wrapper to do this, rather than GREP_OPTIONS as we don't want
# to change grep(1)'s actual behaviour inside scripts
declare -a GREPOPTS
GREPOPTS=()
if ((colors >= 8)) && [[ -n $GREP_COLORS ]] ; then
    GREPOPTS[${#GREPOPTS[@]}]='--color=auto'
fi
if [[ -e $HOME/.cache/grep/binary-files ]] ; then
    GREPOPTS[${#GREPOPTS[@]}]='--binary-files=without-match'
fi
if [[ -e $HOME/.cache/grep/exclude ]] ; then
    GREPOPTS[${#GREPOPTS[@]}]='--exclude={.gitignore,.gitmodules}'
fi
if [[ -e $HOME/.cache/grep/exclude-dir ]] ; then
    GREPOPTS[${#GREPOPTS[@]}]='--exclude-dir={.cvs,.git,.hg,.svn}'
fi

# Done, unset color var
unset -v colors

# Define function proper
grep() {
    command grep "${GREPOPTS[@]}" "$@"
}