gnu-utils.plugin.zsh 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. local -a gcmds
  13. local gcmd
  14. # coreutils
  15. gcmds=('g[' 'gbase64' 'gbasename' 'gcat' 'gchcon' 'gchgrp' 'gchmod'
  16. 'gchown' 'gchroot' 'gcksum' 'gcomm' 'gcp' 'gcsplit' 'gcut' 'gdate'
  17. 'gdd' 'gdf' 'gdir' 'gdircolors' 'gdirname' 'gdu' 'gecho' 'genv' 'gexpand'
  18. 'gexpr' 'gfactor' 'gfalse' 'gfmt' 'gfold' 'ggroups' 'ghead' 'ghostid'
  19. 'gid' 'gindent' 'ginstall' 'gjoin' 'gkill' 'glink' 'gln' 'glogname' 'gls' 'gmd5sum'
  20. 'gmkdir' 'gmkfifo' 'gmknod' 'gmktemp' 'gmv' 'gnice' 'gnl' 'gnohup' 'gnproc'
  21. 'god' 'gpaste' 'gpathchk' 'gpinky' 'gpr' 'gprintenv' 'gprintf' 'gptx' 'gpwd'
  22. 'greadlink' 'grm' 'grmdir' 'gruncon' 'gseq' 'gsha1sum' 'gsha224sum'
  23. 'gsha256sum' 'gsha384sum' 'gsha512sum' 'gshred' 'gshuf' 'gsleep' 'gsort'
  24. 'gsplit' 'gstat' 'gstty' 'gsum' 'gsync' 'gtac' 'gtail' 'gtee' 'gtest'
  25. 'gtimeout' 'gtouch' 'gtr' 'gtrue' 'gtruncate' 'gtsort' 'gtty' 'guname'
  26. 'gunexpand' 'guniq' 'gunlink' 'guptime' 'gusers' 'gvdir' 'gwc' 'gwho'
  27. 'gwhoami' 'gyes')
  28. # findutils
  29. gcmds+=('gfind' 'gxargs' 'glocate')
  30. # Not part of either coreutils or findutils, installed separately.
  31. gcmds+=('gsed' 'gtar' 'gtime' 'gmake' 'ggrep')
  32. # can be built optionally
  33. gcmds+=('ghostname')
  34. for gcmd in "${gcmds[@]}"; do
  35. # Do nothing if the command isn't found
  36. (( ${+commands[$gcmd]} )) || continue
  37. # This method allows for builtin commands to be primary but it's
  38. # lost if hash -r or rehash is executed, or if $PATH is updated.
  39. # Thus, a preexec hook is needed, which will only run if whoami
  40. # is not already rehashed.
  41. #
  42. hash ${gcmd[2,-1]}=${commands[$gcmd]}
  43. done
  44. return 0
  45. }
  46. __gnu_utils_preexec() {
  47. # Run __gnu_utils when the whoami command is not already rehashed.
  48. # This acts as a sign that we need to rehash all GNU utils.
  49. [[ "${commands[whoami]}" = "${commands[gwhoami]}" ]] || __gnu_utils
  50. }
  51. autoload -Uz add-zsh-hook
  52. add-zsh-hook preexec __gnu_utils_preexec
  53. # lib/theme-and-appearance.zsh sets the alias for ls not knowing that
  54. # we'll be using GNU ls. We'll reset this to use GNU ls --color.
  55. # See https://github.com/ohmyzsh/ohmyzsh/issues/11503
  56. #
  57. # The ls alias might look like:
  58. # - ls='ls -G'
  59. # - ls='gls --color=tty'
  60. if [[ -x "${commands[gls]}" && "${aliases[ls]}" = (*-G*|gls*) ]]; then
  61. alias ls='ls --color=tty'
  62. fi