init.zsh 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #
  2. # Defines general aliases and functions.
  3. #
  4. # Authors:
  5. # Robby Russell <robby@planetargon.com>
  6. # Suraj N. Kurapati <sunaku@gmail.com>
  7. # Sorin Ionescu <sorin.ionescu@gmail.com>
  8. #
  9. # Load dependencies.
  10. pmodload 'helper' 'spectrum'
  11. # Correct commands.
  12. if zstyle -T ':prezto:module:utility' correct; then
  13. setopt CORRECT
  14. fi
  15. # Load 'run-help' function.
  16. autoload -Uz run-help-{ip,openssl,sudo}
  17. #
  18. # Aliases
  19. #
  20. # Disable correction.
  21. alias ack='nocorrect ack'
  22. alias cd='nocorrect cd'
  23. alias cp='nocorrect cp'
  24. alias ebuild='nocorrect ebuild'
  25. alias gcc='nocorrect gcc'
  26. alias gist='nocorrect gist'
  27. alias grep='nocorrect grep'
  28. alias heroku='nocorrect heroku'
  29. alias ln='nocorrect ln'
  30. alias man='nocorrect man'
  31. alias mkdir='nocorrect mkdir'
  32. alias mv='nocorrect mv'
  33. alias mysql='nocorrect mysql'
  34. alias rm='nocorrect rm'
  35. # Disable globbing.
  36. alias bower='noglob bower'
  37. alias fc='noglob fc'
  38. alias find='noglob find'
  39. alias ftp='noglob ftp'
  40. alias history='noglob history'
  41. alias locate='noglob locate'
  42. alias rake='noglob rake'
  43. alias rsync='noglob rsync'
  44. alias scp='noglob scp'
  45. alias sftp='noglob sftp'
  46. # Define general aliases.
  47. alias _='sudo'
  48. alias b='${(z)BROWSER}'
  49. alias diffu="diff --unified"
  50. alias e='${(z)VISUAL:-${(z)EDITOR}}'
  51. alias mkdir="${aliases[mkdir]:-mkdir} -p"
  52. alias p='${(z)PAGER}'
  53. alias po='popd'
  54. alias pu='pushd'
  55. alias sa='alias | grep -i'
  56. alias type='type -a'
  57. alias cd..='cd ..'
  58. alias recent='history | grep -v recent | grep $1'
  59. alias home='clear; cd; pwd;ls'
  60. alias here='clear; pwd; ls'
  61. # Safe ops. Ask the user before doing anything destructive.
  62. alias cpi="${aliases[cp]:-cp} -i"
  63. alias lni="${aliases[ln]:-ln} -i"
  64. alias mvi="${aliases[mv]:-mv} -i"
  65. alias rmi="${aliases[rm]:-rm} -i"
  66. if zstyle -T ':prezto:module:utility' safe-ops; then
  67. alias cp="${aliases[cp]:-cp} -i"
  68. alias ln="${aliases[ln]:-ln} -i"
  69. alias mv="${aliases[mv]:-mv} -i"
  70. alias rm="${aliases[rm]:-rm} -i"
  71. fi
  72. # ls
  73. if [[ ${(@M)${(f)"$(ls --version 2>&1)"}:#*(GNU|lsd) *} ]]; then
  74. # GNU Core Utilities
  75. if zstyle -T ':prezto:module:utility:ls' dirs-first; then
  76. alias ls="${aliases[ls]:-ls} --group-directories-first"
  77. fi
  78. if zstyle -t ':prezto:module:utility:ls' color; then
  79. # Define colors for GNU ls if they're not already defined
  80. if (( ! $+LS_COLORS )); then
  81. # Try dircolors when available
  82. if is-callable 'dircolors'; then
  83. eval "$(dircolors --sh $HOME/.dir_colors(N))"
  84. else
  85. export LS_COLORS='di=34:ln=35:so=32:pi=33:ex=31:bd=36;01:cd=33;01:su=31;40;07:sg=36;40;07:tw=32;40;07:ow=33;40;07:'
  86. fi
  87. fi
  88. alias ls="${aliases[ls]:-ls} --color=auto"
  89. else
  90. alias ls="${aliases[ls]:-ls} -F"
  91. fi
  92. else
  93. # BSD Core Utilities
  94. if zstyle -t ':prezto:module:utility:ls' color; then
  95. # Define colors for BSD ls if they're not already defined
  96. if (( ! $+LSCOLORS )); then
  97. export LSCOLORS='exfxcxdxbxGxDxabagacad'
  98. fi
  99. alias ls="${aliases[ls]:-ls} -G"
  100. else
  101. alias ls="${aliases[ls]:-ls} -F"
  102. fi
  103. fi
  104. alias l.='ls -d .*' # Lists hidden files only.
  105. alias l='ls -1A' # Lists in one column, hidden files.
  106. alias ll='ls -lh' # Lists human readable sizes.
  107. alias lr='ll -R' # Lists human readable sizes, recursively.
  108. alias la='ll -A' # Lists human readable sizes, hidden files.
  109. alias lm='la | "$PAGER"' # Lists human readable sizes, hidden files through pager.
  110. alias lk='ll -Sr' # Lists sorted by size, largest last.
  111. alias lt='ll -tr' # Lists sorted by date, most recent last.
  112. alias lc='lt -c' # Lists sorted by date, most recent last, shows change time.
  113. alias lu='lt -u' # Lists sorted by date, most recent last, shows access time.
  114. if [[ ${(@M)${(f)"$(ls --version 2>&1)"}:#*GNU *} ]]; then
  115. alias lx='ll -XB' # Lists sorted by extension (GNU only).
  116. fi
  117. # Grep
  118. if zstyle -t ':prezto:module:utility:grep' color; then
  119. export GREP_COLOR=${GREP_COLOR:-'37;45'} # BSD.
  120. export GREP_COLORS=${GREP_COLORS:-"mt=$GREP_COLOR"} # GNU.
  121. alias grep="${aliases[grep]:-grep} --color=auto"
  122. fi
  123. # macOS Everywhere
  124. if is-darwin; then
  125. alias o='open'
  126. elif is-cygwin; then
  127. alias o='cygstart'
  128. alias pbcopy='tee > /dev/clipboard'
  129. alias pbpaste='cat /dev/clipboard'
  130. elif is-termux; then
  131. alias o='termux-open'
  132. alias pbcopy='termux-clipboard-set'
  133. alias pbpaste='termux-clipboard-get'
  134. else
  135. alias o='xdg-open'
  136. if (( $+commands[xclip] )); then
  137. alias pbcopy='xclip -selection clipboard -in'
  138. alias pbpaste='xclip -selection clipboard -out'
  139. elif (( $+commands[xsel] )); then
  140. alias pbcopy='xsel --clipboard --input'
  141. alias pbpaste='xsel --clipboard --output'
  142. fi
  143. fi
  144. alias pbc='pbcopy'
  145. alias pbp='pbpaste'
  146. # File Download
  147. zstyle -s ':prezto:module:utility:download' helper '_download_helper' || _download_helper='curl'
  148. typeset -A _download_helpers=(
  149. aria2c 'aria2c --continue --remote-time --max-tries=0'
  150. curl 'curl --continue-at - --location --progress-bar --remote-name --remote-time'
  151. wget 'wget --continue --progress=bar --timestamping'
  152. )
  153. if (( $+commands[$_download_helper] && $+_download_helpers[$_download_helper] )); then
  154. alias get="$_download_helpers[$_download_helper]"
  155. elif (( $+commands[curl] )); then
  156. alias get="$_download_helpers[curl]"
  157. fi
  158. unset _download_helper{,s}
  159. # Resource Usage
  160. alias df='df -kh'
  161. alias dfl='df -l'
  162. alias du='du -kh'
  163. if is-darwin || is-bsd; then
  164. alias topc='top -o cpu'
  165. alias topm='top -o vsize'
  166. else
  167. alias topc='top -o %CPU'
  168. alias topm='top -o %MEM'
  169. fi
  170. # Miscellaneous
  171. # Serves a directory via HTTP.
  172. if (( $#commands[(i)python(|[23])] )); then
  173. autoload -Uz is-at-least
  174. if (( $+commands[python3] )); then
  175. alias http-serve='python3 -m http.server'
  176. elif (( $+commands[python2] )); then
  177. alias http-serve='python2 -m SimpleHTTPServer'
  178. elif is-at-least 3 ${"$(python --version 2>&1)"[(w)2]}; then
  179. alias http-serve='python -m http.server'
  180. else
  181. alias http-serve='python -m SimpleHTTPServer'
  182. fi
  183. fi
  184. #
  185. # Functions
  186. #
  187. # Makes a directory and changes to it.
  188. function mkdcd {
  189. [[ -n "$1" ]] && mkdir -p "$1" && builtin cd "$1"
  190. }
  191. # Changes to a directory and lists its contents.
  192. function cdls {
  193. builtin cd "$argv[-1]" && ls "${(@)argv[1,-2]}"
  194. }
  195. # Pushes an entry onto the directory stack and lists its contents.
  196. function pushdls {
  197. builtin pushd "$argv[-1]" && ls "${(@)argv[1,-2]}"
  198. }
  199. # Pops an entry off the directory stack and lists its contents.
  200. function popdls {
  201. builtin popd "$argv[-1]" && ls "${(@)argv[1,-2]}"
  202. }
  203. # Prints columns 1 2 3 ... n.
  204. function slit {
  205. awk "{ print ${(j:,:):-\$${^@}} }"
  206. }
  207. # Finds files and executes a command on them.
  208. function find-exec {
  209. find . -type f -iname "*${1:-}*" -exec "${2:-file}" '{}' \;
  210. }
  211. # Displays user owned processes status.
  212. function psu {
  213. ps -U "${1:-$LOGNAME}" -o 'pid,%cpu,%mem,command' "${(@)argv[2,-1]}"
  214. }
  215. # Enables globbing selectively on path arguments.
  216. # Globbing is enabled on local paths (starting in '/' and './') and disabled
  217. # on remote paths (containing ':' but not starting in '/' and './'). This is
  218. # useful for programs that have their own globbing for remote paths.
  219. # Currently, this is used by default for 'rsync' and 'scp'.
  220. # Example:
  221. # - Local: '*.txt', './foo:2017*.txt', '/var/*:log.txt'
  222. # - Remote: user@localhost:foo/
  223. #
  224. # NOTE: This function is buggy and is not used anywhere until we can make sure
  225. # it's fixed. See https://github.com/sorin-ionescu/prezto/issues/1443 and
  226. # https://github.com/sorin-ionescu/prezto/issues/1521 for more information.
  227. function noremoteglob {
  228. local -a argo
  229. local cmd="$1"
  230. for arg in ${argv:2}; do case $arg in
  231. ( ./* ) argo+=( ${~arg} ) ;; # local relative, glob
  232. ( /* ) argo+=( ${~arg} ) ;; # local absolute, glob
  233. ( *:* ) argo+=( ${arg} ) ;; # remote, noglob
  234. ( * ) argo+=( ${~arg} ) ;; # default, glob
  235. esac; done
  236. command $cmd "${(@)argo}"
  237. }