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() {
# Is grep argument $1 available?
grep-flag-available() {
echo | grep $1 "" >/dev/null 2>&1
}
# Color grep results.
local GREP_OPTIONS="--color=auto"
if grep-flag-available --exclude-dir=.cvs; then
GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS" GREP_OPTIONS+=" --exclude-dir=$VCS_FOLDERS"
elif grep-flag-available --exclude=.cvs; then elif grep-flag-available --exclude=.cvs; then
GREP_OPTIONS+=" --exclude=$VCS_FOLDERS" GREP_OPTIONS+=" --exclude=$VCS_FOLDERS"
fi fi
# export grep settings # Re-define alias.
alias grep="grep $GREP_OPTIONS" alias grep="grep $GREP_OPTIONS"
# clean up # Clean up.
unset GREP_OPTIONS unfunction grep-flag-available
unset VCS_FOLDERS }
unfunction grep-flag-available alias grep=_setup_grep_alias