gnu-utils.plugin.zsh 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # ------------------------------------------------------------------------------
  2. # FILE: gnu-utils.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.0.0
  6. # ------------------------------------------------------------------------------
  7. # Detect if GNU coreutils are installed by looking for gwhoami
  8. if [[ ! -x "${commands[gwhoami]}" ]]; then
  9. return
  10. fi
  11. __gnu_utils() {
  12. emulate -L zsh
  13. local gcmds
  14. local gcmd
  15. local cmd
  16. local prefix
  17. # coreutils
  18. gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
  19. 'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
  20. 'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
  21. 'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
  22. 'gid' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
  23. 'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
  24. 'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
  25. 'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
  26. 'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
  27. 'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
  28. 'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
  29. 'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
  30. 'gwhoami' 'gyes')
  31. # findutils
  32. gcmds+=('gfind' 'gxargs' 'glocate')
  33. # Not part of either coreutils or findutils, installed separately.
  34. gcmds+=('gsed' 'gtar' 'gtime' 'gmake')
  35. for gcmd in "${gcmds[@]}"; do
  36. # Do nothing if the command isn't found
  37. (( ${+commands[$gcmd]} )) || continue
  38. # This method allows for builtin commands to be primary but it's
  39. # lost if hash -r or rehash -f is executed. Thus, those two
  40. # functions have to be wrapped.
  41. #
  42. hash ${gcmd[2,-1]}=${commands[$gcmd]}
  43. # This method generates wrapper functions.
  44. # It will override shell builtins.
  45. #
  46. # eval "function $gcmd[2,-1]() { \"${prefix}/${gcmd//"["/"\\["}\" \"\$@\"; }"
  47. # This method is inflexible since the aliases are at risk of being
  48. # overridden resulting in the BSD coreutils being called.
  49. #
  50. # alias "$gcmd[2,-1]"="${prefix}/${gcmd//"["/"\\["}"
  51. done
  52. return 0
  53. }
  54. __gnu_utils
  55. function hash() {
  56. if [[ "$*" =~ "-(r|f)" ]]; then
  57. builtin hash "$@"
  58. __gnu_utils
  59. else
  60. builtin hash "$@"
  61. fi
  62. }
  63. function rehash() {
  64. if [[ "$*" =~ "-f" ]]; then
  65. builtin rehash "$@"
  66. __gnu_utils
  67. else
  68. builtin rehash "$@"
  69. fi
  70. }