grep.zsh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # see if we already cached the grep alias in past day
  2. _grep_alias_cache=("$ZSH_CACHE_DIR"/grep_alias.zsh(Nm-24))
  3. if (( $#_grep_alias_cache )); then
  4. source "$ZSH_CACHE_DIR"/grep_alias.zsh
  5. else
  6. # is x grep argument available?
  7. grep-flags-available() {
  8. echo | grep "$@" "" >/dev/null 2>&1
  9. }
  10. GREP_OPTIONS=""
  11. # ignore these folders (if the necessary grep flags are available)
  12. EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
  13. if grep-flags-available --color=auto --exclude-dir=.cvs; then
  14. GREP_OPTIONS+="--color=auto --exclude-dir=$EXC_FOLDERS"
  15. elif grep-flags-available --color=auto --exclude=.cvs; then
  16. GREP_OPTIONS+="--color=auto --exclude=$EXC_FOLDERS"
  17. elif grep-flags-available --color=auto; then
  18. GREP_OPTIONS+="--color=auto"
  19. fi
  20. {
  21. if [[ -n "$GREP_OPTIONS" ]]; then
  22. # export grep, egrep and fgrep settings
  23. echo alias grep="'grep $GREP_OPTIONS'"
  24. echo alias egrep="'egrep $GREP_OPTIONS'"
  25. echo alias fgrep="'fgrep $GREP_OPTIONS'"
  26. fi
  27. } > "$ZSH_CACHE_DIR/grep_alias.zsh"
  28. source "$ZSH_CACHE_DIR/grep_alias.zsh"
  29. # clean up
  30. unset GREP_OPTIONS EXC_FOLDERS
  31. unfunction grep-flags-available
  32. fi
  33. unset _grep_alias_cache