grep.zsh 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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-flag-available() {
  8. echo | grep $1 "" >/dev/null 2>&1
  9. }
  10. GREP_OPTIONS=""
  11. # color grep results
  12. if grep-flag-available --color=auto; then
  13. GREP_OPTIONS+=" --color=auto"
  14. fi
  15. # ignore these folders (if the necessary grep flags are available)
  16. EXC_FOLDERS="{.bzr,CVS,.git,.hg,.svn,.idea,.tox}"
  17. if grep-flag-available --exclude-dir=.cvs; then
  18. GREP_OPTIONS+=" --exclude-dir=$EXC_FOLDERS"
  19. elif grep-flag-available --exclude=.cvs; then
  20. GREP_OPTIONS+=" --exclude=$EXC_FOLDERS"
  21. fi
  22. {
  23. # export grep, egrep and fgrep settings
  24. echo alias grep="'grep $GREP_OPTIONS'"
  25. echo alias egrep="'egrep $GREP_OPTIONS'"
  26. echo alias fgrep="'fgrep $GREP_OPTIONS'"
  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-flag-available
  32. fi
  33. unset _grep_alias_cache