init.zsh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #
  2. # Sets completion options.
  3. #
  4. # Authors:
  5. # Robby Russell <robby@planetargon.com>
  6. # Sorin Ionescu <sorin.ionescu@gmail.com>
  7. #
  8. # Return if requirements are not found.
  9. if [[ $TERM == 'dumb' ]]; then
  10. return 1
  11. fi
  12. # Add zsh-completions to $fpath.
  13. fpath=(${0:h}/external/src $fpath)
  14. # Add completion for keg-only brewed curl on macOS when available.
  15. if (( $+commands[brew] )); then
  16. brew_prefix=${HOMEBREW_PREFIX:-${HOMEBREW_REPOSITORY:-$commands[brew]:A:h:h}}
  17. # $HOMEBREW_PREFIX defaults to $HOMEBREW_REPOSITORY but is explicitly set to
  18. # /usr/local when $HOMEBREW_REPOSITORY is /usr/local/Homebrew.
  19. # https://github.com/Homebrew/brew/blob/2a850e02d8f2dedcad7164c2f4b95d340a7200bb/bin/brew#L66-L69
  20. [[ $brew_prefix == '/usr/local/Homebrew' ]] && brew_prefix=$brew_prefix:h
  21. fpath=($brew_prefix/opt/curl/share/zsh/site-functions(/N) $fpath)
  22. unset brew_prefix
  23. fi
  24. #
  25. # Options
  26. #
  27. setopt COMPLETE_IN_WORD # Complete from both ends of a word.
  28. setopt ALWAYS_TO_END # Move cursor to the end of a completed word.
  29. setopt PATH_DIRS # Perform path search even on command names with slashes.
  30. setopt AUTO_MENU # Show completion menu on a successive tab press.
  31. setopt AUTO_LIST # Automatically list choices on ambiguous completion.
  32. setopt AUTO_PARAM_SLASH # If completed parameter is a directory, add a trailing slash.
  33. setopt EXTENDED_GLOB # Needed for file modification glob modifiers with compinit.
  34. unsetopt MENU_COMPLETE # Do not autoselect the first completion entry.
  35. unsetopt FLOW_CONTROL # Disable start/stop characters in shell editor.
  36. #
  37. # Variables
  38. #
  39. # Standard style used by default for 'list-colors'
  40. LS_COLORS=${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:'}
  41. #
  42. # Initialization
  43. #
  44. # Load and initialize the completion system ignoring insecure directories with a
  45. # cache time of 20 hours, so it should almost always regenerate the first time a
  46. # shell is opened each day.
  47. autoload -Uz compinit
  48. _comp_path="${XDG_CACHE_HOME:-$HOME/.cache}/prezto/zcompdump"
  49. # #q expands globs in conditional expressions
  50. if [[ $_comp_path(#qNmh-20) ]]; then
  51. # -C (skip function check) implies -i (skip security check).
  52. compinit -C -d "$_comp_path"
  53. else
  54. mkdir -p "$_comp_path:h"
  55. compinit -i -d "$_comp_path"
  56. # Keep $_comp_path younger than cache time even if it isn't regenerated.
  57. touch "$_comp_path"
  58. fi
  59. unset _comp_path
  60. #
  61. # Styles
  62. #
  63. # Defaults.
  64. zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
  65. zstyle ':completion:*:default' list-prompt '%S%M matches%s'
  66. # Use caching to make completion for commands such as dpkg and apt usable.
  67. zstyle ':completion::complete:*' use-cache on
  68. zstyle ':completion::complete:*' cache-path "${XDG_CACHE_HOME:-$HOME/.cache}/prezto/zcompcache"
  69. # Case-insensitive (all), partial-word, and then substring completion.
  70. if zstyle -t ':prezto:module:completion:*' case-sensitive; then
  71. zstyle ':completion:*' matcher-list 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
  72. setopt CASE_GLOB
  73. else
  74. zstyle ':completion:*' matcher-list 'm:{[:lower:]}={[:upper:]}' 'm:{[:upper:]}={[:lower:]}' 'r:|[._-]=* r:|=*' 'l:|=* r:|=*'
  75. unsetopt CASE_GLOB
  76. fi
  77. # Group matches and describe.
  78. zstyle ':completion:*:*:*:*:*' menu select
  79. zstyle ':completion:*:matches' group 'yes'
  80. zstyle ':completion:*:options' description 'yes'
  81. zstyle ':completion:*:options' auto-description '%d'
  82. zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
  83. zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
  84. zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
  85. zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
  86. zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
  87. zstyle ':completion:*' group-name ''
  88. zstyle ':completion:*' verbose yes
  89. # Fuzzy match mistyped completions.
  90. zstyle ':completion:*' completer _complete _match _approximate
  91. zstyle ':completion:*:match:*' original only
  92. zstyle ':completion:*:approximate:*' max-errors 1 numeric
  93. # Increase the number of errors based on the length of the typed word. But make
  94. # sure to cap (at 7) the max-errors to avoid hanging.
  95. zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3>7?7:($#PREFIX+$#SUFFIX)/3))numeric)'
  96. # Don't complete unavailable commands.
  97. zstyle ':completion:*:functions' ignored-patterns '(_*|pre(cmd|exec))'
  98. # Array completion element sorting.
  99. zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
  100. # Directories
  101. zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
  102. zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
  103. zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
  104. zstyle ':completion:*' squeeze-slashes true
  105. # History
  106. zstyle ':completion:*:history-words' stop yes
  107. zstyle ':completion:*:history-words' remove-all-dups yes
  108. zstyle ':completion:*:history-words' list false
  109. zstyle ':completion:*:history-words' menu yes
  110. # Environment Variables
  111. zstyle ':completion::*:(-command-|export):*' fake-parameters ${${${_comps[(I)-value-*]#*,}%%,*}:#-*-}
  112. # Populate hostname completion. But allow ignoring custom entries from static
  113. # */etc/hosts* which might be uninteresting.
  114. zstyle -a ':prezto:module:completion:*:hosts' etc-host-ignores '_etc_host_ignores'
  115. zstyle -e ':completion:*:hosts' hosts 'reply=(
  116. ${=${=${=${${(f)"$(cat {/etc/ssh/ssh_,~/.ssh/}known_hosts(|2)(N) 2> /dev/null)"}%%[#| ]*}//\]:[0-9]*/ }//,/ }//\[/ }
  117. ${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2> /dev/null))"}%%(\#${_etc_host_ignores:+|${(j:|:)~_etc_host_ignores}})*}
  118. ${=${${${${(@M)${(f)"$(cat ~/.ssh/config 2> /dev/null)"}:#Host *}#Host }:#*\**}:#*\?*}}
  119. )'
  120. # Don't complete uninteresting users...
  121. zstyle ':completion:*:*:*:users' ignored-patterns \
  122. adm amanda apache avahi beaglidx bin cacti canna clamav daemon \
  123. dbus distcache dovecot fax ftp games gdm gkrellmd gopher \
  124. hacluster haldaemon halt hsqldb ident junkbust ldap lp mail \
  125. mailman mailnull mldonkey mysql nagios \
  126. named netdump news nfsnobody nobody nscd ntp nut nx openvpn \
  127. operator pcap postfix postgres privoxy pulse pvm quagga radvd \
  128. rpc rpcuser rpm shutdown squid sshd sync uucp vcsa xfs '_*'
  129. # ... unless we really want to.
  130. zstyle '*' single-ignored show
  131. # Ignore multiple entries.
  132. zstyle ':completion:*:(rm|kill|diff):*' ignore-line other
  133. zstyle ':completion:*:rm:*' file-patterns '*:all-files'
  134. # Kill
  135. zstyle ':completion:*:*:*:*:processes' command 'ps -u $LOGNAME -o pid,user,command -w'
  136. zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;36=0=01'
  137. zstyle ':completion:*:*:kill:*' menu yes select
  138. zstyle ':completion:*:*:kill:*' force-list always
  139. zstyle ':completion:*:*:kill:*' insert-ids single
  140. # Man
  141. zstyle ':completion:*:manuals' separate-sections true
  142. zstyle ':completion:*:manuals.(^1*)' insert-sections true
  143. # Media Players
  144. zstyle ':completion:*:*:mpg123:*' file-patterns '*.(mp3|MP3):mp3\ files *(-/):directories'
  145. zstyle ':completion:*:*:mpg321:*' file-patterns '*.(mp3|MP3):mp3\ files *(-/):directories'
  146. zstyle ':completion:*:*:ogg123:*' file-patterns '*.(ogg|OGG|flac):ogg\ files *(-/):directories'
  147. zstyle ':completion:*:*:mocp:*' file-patterns '*.(wav|WAV|mp3|MP3|ogg|OGG|flac):ogg\ files *(-/):directories'
  148. # Mutt
  149. if [[ -s "$HOME/.mutt/aliases" ]]; then
  150. zstyle ':completion:*:*:mutt:*' menu yes select
  151. zstyle ':completion:*:mutt:*' users ${${${(f)"$(<"$HOME/.mutt/aliases")"}#alias[[:space:]]}%%[[:space:]]*}
  152. fi
  153. # SSH/SCP/RSYNC
  154. zstyle ':completion:*:(ssh|scp|rsync):*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *'
  155. zstyle ':completion:*:(scp|rsync):*' group-order users files all-files hosts-domain hosts-host hosts-ipaddr
  156. zstyle ':completion:*:ssh:*' group-order users hosts-domain hosts-host users hosts-ipaddr
  157. zstyle ':completion:*:(ssh|scp|rsync):*:hosts-host' ignored-patterns '*(.|:)*' loopback ip6-loopback localhost ip6-localhost broadcasthost
  158. zstyle ':completion:*:(ssh|scp|rsync):*:hosts-domain' ignored-patterns '<->.<->.<->.<->' '^[-[:alnum:]]##(.[-[:alnum:]]##)##' '*@*'
  159. zstyle ':completion:*:(ssh|scp|rsync):*:hosts-ipaddr' ignored-patterns '^(<->.<->.<->.<->|(|::)([[:xdigit:].]##:(#c,2))##(|%*))' '127.0.0.<->' '255.255.255.255' '::1' 'fe80::*'