Skip to content

Instantly share code, notes, and snippets.

@dserodio
Created April 22, 2024 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dserodio/ed580f145565f33248fff1ea11f59d8c to your computer and use it in GitHub Desktop.
Save dserodio/ed580f145565f33248fff1ea11f59d8c to your computer and use it in GitHub Desktop.
Zsh Completions

zsh completion

How to look up completion definitions

The name of the completion function for the command foo is $_comps[foo].

To see the code of a function myfunc, run echo -E $functions[myfunc], or just echo $functions[myfunc] if you have the bsd_echo option on, or print -rl $functions[myfunc]. So to see the code of the completion function for the command foo, run echo -E $functions[$_comps[foo]]. Alternatively, run which $_comps[foo] if the function name has no alias.

This shows the code without comments (and with normalized whitespace: it's a human-readable dump of the bytecode that zsh stores internally). If you want to see the original file defined in the code, run whence -v $_comps[foo] or echo $functions_source[$_comps[foo]]. The functions_source array is only available if the module zsh/parameter is loaded (you can do this with zmodload zsh/parameter) and only since zsh 5.4.

If you haven't used the function yet, you'll see something like builtin autoload -XU instead of the code, and no path to the source. To see the path to the source, run autoload -r $_comps[foo] first to make zsh resolve the path to the source, then you can display it with which or whence or $functions_source.

zmodload zsh/parameter
(($+_comps[foo])) &&
autoload -r $_comps[foo] &&
((+$functions_source[$_comps[foo]])) &&
cat $functions_source[$_comps[foo]]

Source: https://unix.stackexchange.com/a/509944/7214

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment