Setup grep alias lazily through a function

This saves an extra / initial call to `grep` during shell startup.
This commit is contained in:
Daniel Hahler 2015-01-07 03:34:22 +01:00
commit fec736f7f6

View file

@ -1,24 +1,25 @@
# is x grep argument available? # Ignore VCS folders (if the necessary grep flags are available).
grep-flag-available() {
echo | grep $1 "" >/dev/null 2>&1
}
# color grep results
GREP_OPTIONS="--color=auto"
# ignore VCS folders (if the necessary grep flags are available)
VCS_FOLDERS="{.bzr,.cvs,.git,.hg,.svn}" VCS_FOLDERS="{.bzr,.cvs,.git,.hg,.svn}"
if grep-flag-available --exclude-dir=.cvs; then _setup_grep_alias() {
GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" # Is grep argument $1 available?
elif grep-flag-available --exclude=.cvs; then grep-flag-available() {
GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" echo | grep $1 "" >/dev/null 2>&1
fi }
# export grep settings # Color grep results.
alias grep="grep $GREP_OPTIONS" local GREP_OPTIONS="--color=auto"
# clean up if grep-flag-available --exclude-dir=.cvs; then
unset GREP_OPTIONS GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
unset VCS_FOLDERS elif grep-flag-available --exclude=.cvs; then
unfunction grep-flag-available GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
fi
# Re-define alias.
alias grep="grep $GREP_OPTIONS"
# Clean up.
unfunction grep-flag-available
}
alias grep=_setup_grep_alias