3 | awk '{ CMD[$2]++; count++; } END { for (a in CMD) print CMD[a] " " CMD[a]*100/count "% " a }' \
4 | grep -v "./" | sort -nr | head -n 20 | column -c3 -s " " -t | nl
7 function uninstall_oh_my_zsh() {
8 env ZSH="$ZSH" sh "$ZSH/tools/uninstall.sh"
11 function upgrade_oh_my_zsh() {
12 echo >&2 "${fg[yellow]}Note: \`$0\` is deprecated. Use \`omz update\` instead.$reset_color"
16 function open_command() {
19 # define the open command
21 darwin*) open_cmd='open' ;;
22 cygwin*) open_cmd='cygstart' ;;
23 linux*) [[ "$(uname -r)" != *icrosoft* ]] && open_cmd='nohup xdg-open' || {
24 open_cmd='cmd.exe /c start ""'
25 [[ -e "$1" ]] && { 1="$(wslpath -w "${1:a}")" || return 1 }
27 msys*) open_cmd='start ""' ;;
28 *) echo "Platform $OSTYPE not supported"
33 ${=open_cmd} "$@" &>/dev/null
38 # mkcd is equivalent to takedir
39 function mkcd takedir() {
40 mkdir -p $@ && cd ${@:$#}
46 curl -L "$1" > "$data"
48 thedir="$(tar tf "$data" | head -n 1)"
55 cd "$(basename ${1%%.git})"
59 if [[ $1 =~ ^(https?|ftp).*\.tar\.(gz|bz2|xz)$ ]]; then
61 elif [[ $1 =~ ^([A-Za-z0-9]\+@|https?|git|ssh|ftps?|rsync).*\.git/?$ ]]; then
69 # Get the value of an alias.
72 # 1. alias - The alias to get its value from
74 # The value of alias $1 (if it has one).
76 # 0 if the alias was found,
77 # 1 if it does not exist
79 function alias_value() {
80 (( $+aliases[$1] )) && echo $aliases[$1]
84 # Try to get the value of an alias,
85 # otherwise return the input.
88 # 1. alias - The alias to get its value from
90 # The value of alias $1, or $1 if there is no alias $1.
94 function try_alias_value() {
95 alias_value "$1" || echo "$1"
99 # Set variable "$1" to default value "$2" if "$1" is not yet defined.
102 # 1. name - The variable to set
103 # 2. val - The default value
105 # 0 if the variable exists, 3 if it was set
108 (( $+parameters[$1] )) && return 0
109 typeset -g "$1"="$2" && return 3
113 # Set environment variable "$1" to default value "$2" if "$1" is not yet defined.
116 # 1. name - The env variable to set
117 # 2. val - The default value
119 # 0 if the env variable exists, 3 if it was set
121 function env_default() {
122 [[ ${parameters[$1]} = *-export* ]] && return 0
123 export "$1=$2" && return 3
127 # Required for $langinfo
128 zmodload zsh/langinfo
130 # URL-encode a string
132 # Encodes a string using RFC 2396 URL-encoding (%-escaped).
133 # See: https://www.ietf.org/rfc/rfc2396.txt
135 # By default, reserved characters and unreserved "mark" characters are
136 # not escaped by this function. This allows the common usage of passing
137 # an entire URL in, and encoding just special characters in it, with
138 # the expectation that reserved and mark characters are used appropriately.
139 # The -r and -m options turn on escaping of the reserved and mark characters,
140 # respectively, which allows arbitrary strings to be fully escaped for
141 # embedding inside URLs, where reserved characters might be misinterpreted.
143 # Prints the encoded string on stdout.
144 # Returns nonzero if encoding failed.
147 # omz_urlencode [-r] [-m] [-P] <string> [<string> ...]
149 # -r causes reserved characters (;/?:@&=+$,) to be escaped
151 # -m causes "mark" characters (_.!~*''()-) to be escaped
153 # -P causes spaces to be encoded as '%20' instead of '+'
154 function omz_urlencode() {
157 zparseopts -D -E -a opts r m P
162 if [[ -z $opts[(r)-P] ]]; then spaces_as_plus=1; fi
165 # URLs must use UTF-8 encoding; convert str to UTF-8 if required
166 local encoding=$langinfo[CODESET]
168 safe_encodings=(UTF-8 utf8 US-ASCII)
169 if [[ -z ${safe_encodings[(r)$encoding]} ]]; then
170 str=$(echo -E "$str" | iconv -f $encoding -t UTF-8)
171 if [[ $? != 0 ]]; then
172 echo "Error converting string from $encoding to UTF-8" >&2
177 # Use LC_CTYPE=C to process text byte-by-byte
178 local i byte ord LC_ALL=C
180 local reserved=';/?:@&=+$,'
181 local mark='_.!~*''()-'
182 local dont_escape="[A-Za-z0-9"
183 if [[ -z $opts[(r)-r] ]]; then
184 dont_escape+=$reserved
186 # $mark must be last because of the "-"
187 if [[ -z $opts[(r)-m] ]]; then
192 # Implemented to use a single printf call and avoid subshells in the loop,
193 # for performance (primarily on Windows).
195 for (( i = 1; i <= ${#str}; ++i )); do
197 if [[ "$byte" =~ "$dont_escape" ]]; then
200 if [[ "$byte" == " " && -n $spaces_as_plus ]]; then
203 ord=$(( [##16] #byte ))
211 # URL-decode a string
213 # Decodes a RFC 2396 URL-encoded (%-escaped) string.
214 # This decodes the '+' and '%' escapes in the input string, and leaves
215 # other characters unchanged. Does not enforce that the input is a
216 # valid URL-encoded string. This is a convenience to allow callers to
217 # pass in a full URL or similar strings and decode them for human
220 # Outputs the encoded string on stdout.
221 # Returns nonzero if encoding failed.
224 # omz_urldecode <urlstring> - prints decoded string followed by a newline
225 function omz_urldecode {
229 # Work bytewise, since URLs escape UTF-8 octets
230 local caller_encoding=$langinfo[CODESET]
234 # Change + back to ' '
235 local tmp=${encoded_url:gs/+/ /}
236 # Protect other escapes to pass through the printf unchanged
237 tmp=${tmp:gs/\\/\\\\/}
238 # Handle %-escapes by turning them into `\xXX` printf escapes
240 local decoded="$(printf -- "$tmp")"
242 # Now we have a UTF-8 encoded string in the variable. We need to re-encode
243 # it if caller is in a non-UTF-8 locale.
244 local -a safe_encodings
245 safe_encodings=(UTF-8 utf8 US-ASCII)
246 if [[ -z ${safe_encodings[(r)$caller_encoding]} ]]; then
247 decoded=$(echo -E "$decoded" | iconv -f UTF-8 -t $caller_encoding)
248 if [[ $? != 0 ]]; then
249 echo "Error converting string from UTF-8 to $caller_encoding" >&2