main
1# Deferred completion system — runs on first prompt, not at startup
2# Saves ~53ms by deferring compinit to precmd
3
4autoload -Uz compinit
5
6__deferred_compinit() {
7 local dump=${XDG_CACHE_HOME:-$HOME/.cache}/zsh/.zcompdump
8 mkdir -p "${dump:h}" 2>/dev/null
9
10 if [[ ! -f $dump ]]; then
11 compinit -d "$dump"
12 else
13 compinit -C -d "$dump" # -C skips security check (single-user machine)
14 fi
15 _zsh_compile_if_needed "$dump"
16
17 # Wire up alias expansion (needs compinit)
18 # ctrl-x ctrl-a to expand aliases in place (ctrl-a stays as beginning-of-line)
19 zle -C alias-expansion complete-word _generic
20 bindkey '^x^a' alias-expansion
21 zstyle ':completion:alias-expansion:*' completer _expand_alias
22
23 add-zsh-hook -d precmd __deferred_compinit
24 unfunction __deferred_compinit
25}
26autoload -Uz add-zsh-hook
27add-zsh-hook precmd __deferred_compinit
28
29# Completion styles (zstyle declarations are cheap — no need to defer)
30zstyle ':completion:*' menu select
31zstyle ':completion::complete:*' use-cache on
32zstyle ':completion::complete:*' cache-path "${XDG_CACHE_HOME:-$HOME/.cache}/zsh/zcompcache"
33
34# Case-insensitive, partial-word, substring completion
35zstyle ':completion:*' matcher-list '' \
36 'm:{a-z\-}={A-Z\_}' \
37 'r:[^[:alpha:]]||[[:alpha:]]=** r:|=* m:{a-z\-}={A-Z\_}' \
38 'r:[[:ascii:]]||[[:ascii:]]=** r:|=* m:{a-z\-}={A-Z\_}'
39
40# Group matches and describe
41zstyle ':completion:*:*:*:*:*' menu select
42zstyle ':completion:*:matches' group 'yes'
43zstyle ':completion:*:options' description 'yes'
44zstyle ':completion:*:options' auto-description '%d'
45zstyle ':completion:*:corrections' format ' %F{green}-- %d (errors: %e) --%f'
46zstyle ':completion:*:descriptions' format ' %F{yellow}-- %d --%f'
47zstyle ':completion:*:messages' format ' %F{purple} -- %d --%f'
48zstyle ':completion:*:warnings' format ' %F{red}-- no matches found --%f'
49zstyle ':completion:*:default' list-prompt '%S%M matches%s'
50zstyle ':completion:*' format ' %F{yellow}-- %d --%f'
51zstyle ':completion:*' group-name ''
52zstyle ':completion:*' verbose yes
53
54# Fuzzy match mistyped completions
55zstyle ':completion:*' completer _expand_alias _complete _list _match _approximate
56zstyle ':completion:*:match:*' original only
57zstyle ':completion:*:approximate:*' max-errors 1 numeric
58zstyle -e ':completion:*:approximate:*' max-errors 'reply=($((($#PREFIX+$#SUFFIX)/3))numeric)'
59
60# Don't complete unavailable commands
61zstyle ':completion:*:functions' ignored-patterns '(_*|pre(cmd|exec))'
62
63# Array completion element sorting
64zstyle ':completion:*:*:-subscript-:*' tag-order indexes parameters
65
66# Directories
67zstyle ':completion:*:default' list-colors ${(s.:.)LS_COLORS}
68zstyle ':completion:*:*:cd:*' ignore-parents parent pwd
69zstyle ':completion:*:*:cd:*' tag-order local-directories directory-stack path-directories
70zstyle ':completion:*:*:cd:*:directory-stack' menu yes select
71zstyle ':completion:*:-tilde-:*' group-order 'named-directories' 'path-directories' 'users' 'expand'
72zstyle ':completion:*' squeeze-slashes true
73zstyle ':completion:*' special-dirs true
74
75# Accept current item and stay in menu to pick more (+)
76zmodload -i zsh/complist
77bindkey -M menuselect "+" accept-and-menu-complete
78
79# Environmental variables
80zstyle ':completion::*:(-command-|export):*' fake-parameters ${${${_comps[(I)-value-*]#*,}%%,*}:#-*-}
81
82# Populate hostname completion
83zstyle -e ':completion:*:hosts' hosts 'reply=(
84 ${=${=${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) 2>/dev/null)"}%%[#| ]*}//\]:[0-9]*/ }//,/ }//\[/ }
85 ${=${${${${(@M)${(f)"$(cat ~/.ssh/config 2>/dev/null)"}:#Host *}#Host }:#*\**}:#*\?*}}
86)'
87
88# Don't complete uninteresting users
89zstyle ':completion:*:*:*:users' ignored-patterns \
90 adm amanda apache avahi beaglidx bin cacti canna clamav daemon \
91 dbus distcache dovecot fax ftp games gdm gkrellmd gopher \
92 hacluster haldaemon halt hsqldb ident junkbust ldap lp mail \
93 mailman mailnull mldonkey mysql nagios \
94 named netdump news nfsnobody nobody nscd ntp nut nx openvpn \
95 operator pcap postfix postgres privoxy pulse pvm quagga radvd \
96 rpc rpcuser rpm shutdown squid sshd sync uucp vcsa xfs '_*'
97zstyle '*' single-ignored show
98
99# Ignore multiple entries
100zstyle ':completion:*:(rm|kill|diff):*' ignore-line other
101zstyle ':completion:*:rm:*' file-patterns '*:all-files'
102
103# Kill completion with colored PIDs
104zstyle ':completion:*:processes' command 'ps -eo pid,user,comm,cmd -w -w'
105zstyle ':completion:*:*:kill:*:processes' list-colors '=(#b) #([0-9]#) ([0-9a-z-]#)*=01;34=0=01'
106zstyle ':completion:*:kill:*' force-list always
107
108# Man
109zstyle ':completion:*:manuals' separate-sections true
110zstyle ':completion:*:manuals.(^1*)' insert-sections true
111
112# SSH/SCP/RSYNC
113zstyle ':completion:*:(scp|rsync):*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *'
114zstyle ':completion:*:(scp|rsync):*' group-order users files all-files hosts-domain hosts-host hosts-ipaddr
115zstyle ':completion:*:ssh:*' tag-order 'hosts:-host:host hosts:-domain:domain hosts:-ipaddr:ip\ address *'
116zstyle ':completion:*:ssh:*' group-order users hosts-domain hosts-host users hosts-ipaddr
117zstyle ':completion:*:(ssh|scp|rsync):*:hosts-host' ignored-patterns '*(.|:)*' loopback ip6-loopback localhost ip6-localhost broadcasthost
118zstyle ':completion:*:(ssh|scp|rsync):*:hosts-domain' ignored-patterns '<->.<->.<->.<->' '^[-[:alnum:]]##(.[-[:alnum:]]##)##' '*@*'
119zstyle ':completion:*:(ssh|scp|rsync):*:hosts-ipaddr' ignored-patterns '^(<->.<->.<->.<->|(|::)([[:xdigit:].]##:(#c,2))##(|%*))' '127.0.0.<->' '255.255.255.255' '::1' 'fe80::*'