functions.zsh 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. function zsh_stats() {
  2. fc -l 1 | 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. env ZSH=$ZSH /bin/sh $ZSH/tools/uninstall.sh
  6. }
  7. function upgrade_oh_my_zsh() {
  8. env ZSH=$ZSH /bin/sh $ZSH/tools/upgrade.sh
  9. }
  10. function take() {
  11. mkdir -p $1
  12. cd $1
  13. }
  14. function open_command() {
  15. local open_cmd
  16. # define the open command
  17. case "$OSTYPE" in
  18. darwin*) open_cmd="open" ;;
  19. cygwin*) open_cmd="cygstart" ;;
  20. linux*) open_cmd="xdg-open" ;;
  21. *) echo "Platform $OSTYPE not supported"
  22. return 1
  23. ;;
  24. esac
  25. nohup $open_cmd "$@" &>/dev/null
  26. }
  27. #
  28. # Get the value of an alias.
  29. #
  30. # Arguments:
  31. # 1. alias - The alias to get its value from
  32. # STDOUT:
  33. # The value of alias $1 (if it has one).
  34. # Return value:
  35. # 0 if the alias was found,
  36. # 1 if it does not exist
  37. #
  38. function alias_value() {
  39. alias "$1" | sed "s/^$1='\(.*\)'$/\1/"
  40. test $(alias "$1")
  41. }
  42. #
  43. # Try to get the value of an alias,
  44. # otherwise return the input.
  45. #
  46. # Arguments:
  47. # 1. alias - The alias to get its value from
  48. # STDOUT:
  49. # The value of alias $1, or $1 if there is no alias $1.
  50. # Return value:
  51. # Always 0
  52. #
  53. function try_alias_value() {
  54. alias_value "$1" || echo "$1"
  55. }
  56. #
  57. # Set variable "$1" to default value "$2" if "$1" is not yet defined.
  58. #
  59. # Arguments:
  60. # 1. name - The variable to set
  61. # 2. val - The default value
  62. # Return value:
  63. # 0 if the variable exists, 3 if it was set
  64. #
  65. function default() {
  66. test `typeset +m "$1"` && return 0
  67. typeset -g "$1"="$2" && return 3
  68. }
  69. #
  70. # Set enviroment variable "$1" to default value "$2" if "$1" is not yet defined.
  71. #
  72. # Arguments:
  73. # 1. name - The env variable to set
  74. # 2. val - The default value
  75. # Return value:
  76. # 0 if the env variable exists, 3 if it was set
  77. #
  78. function env_default() {
  79. env | grep -q "^$1=" && return 0
  80. export "$1=$2" && return 3
  81. }