How to list all zsh autocompletions?
The list of known completions is stored in the associative array _comps
. The command names and other completion contexts are used as keys for _comps
, while the corresponding completion functions are stored as values.
You can get a full list of commands with associated completions with the following command:
for command completion in ${(kv)_comps:#-*(-|-,*)}do printf "%-32s %s\n" $command $completiondone | sort
Explanation:
for command completion in LIST; COMMAND
takes iterates overLIST
while taking two elements,command
andcompletion
, on every iteration and runningCOMMAND
for them. This is also a short form of thefor
-loop that does not requiredo
anddone
.${(kv)ASSOC_ARRAY}
expands the associative arrayASSOC_ARRAY
to a space separated list of key-value pairs. So it is an alternating list of "key1 value1 key2 value2 key3 value3 …", which is taken up by the two arguments of thefor
-loop.$ASSOC_ARRAY
would only expand to a list of values.${ASSOC_ARRAY:#PATTERN}
filters out all elements ofASSOC_ARRAY
from its expansion, where the key matchesPATTERN
.- The pattern
-*(-|-,*)
matches the names of all special contexts, like-math-
,-parameter-
or-value-,NAME,COMMAND
. It would also filter any command name that either matches-*-
or-*-,*
, should such a command have a completion on your system. (You could just leave out the pattern filter to be sure) printf "%-32s %s\n" $command $completion
does a formatted output so that you get a nice table.$command
is printed in place of%-32s
, padded to 32 characters with left-alignment (-
).$completion
is printed in place of%s
.| sort
: associative arrays are unordered, so output of the loop needs to be run throughsort
in order to get a ordered list.