functions.zsh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function zsh_stats() {
  2. history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n20
  3. }
  4. function uninstall_oh_my_zsh() {
  5. /usr/bin/env ZSH=$ZSH /bin/sh $ZSH/tools/uninstall.sh
  6. }
  7. function upgrade_oh_my_zsh() {
  8. /usr/bin/env ZSH=$ZSH /bin/sh $ZSH/tools/upgrade.sh
  9. }
  10. function take() {
  11. mkdir -p $1
  12. cd $1
  13. }
  14. #
  15. # Get the value of an alias.
  16. #
  17. # Arguments:
  18. # 1. alias - The alias to get its value from
  19. # STDOUT:
  20. # The value of alias $1 (if it has one).
  21. # Return value:
  22. # 0 if the alias was found,
  23. # 1 if it does not exist
  24. #
  25. function alias_value() {
  26. alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
  27. test $(alias "$1")
  28. }
  29. #
  30. # Try to get the value of an alias,
  31. # otherwise return the input.
  32. #
  33. # Arguments:
  34. # 1. alias - The alias to get its value from
  35. # STDOUT:
  36. # The value of alias $1, or $1 if there is no alias $1.
  37. # Return value:
  38. # Always 0
  39. #
  40. function try_alias_value() {
  41. alias_value "$1" || echo "$1"
  42. }
  43. #
  44. # Set variable "$1" to default value "$2" if "$1" is not yet defined.
  45. #
  46. # Arguments:
  47. # 1. name - The variable to set
  48. # 2. val - The default value
  49. # Return value:
  50. # 0 if the variable exists, 3 if it was set
  51. #
  52. function default() {
  53. test `typeset +m "$1"` && return 0
  54. typeset -g "$1"="$2" && return 3
  55. }
  56. #
  57. # Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
  58. #
  59. # Arguments:
  60. # 1. name - The env variable to set
  61. # 2. val - The default value
  62. # Return value:
  63. # 0 if the env variable exists, 3 if it was set
  64. #
  65. function env_default() {
  66. env | grep -q "^$1=" && return 0
  67. export "$1=$2" && return 3
  68. }