I'm trying to make use of zsh's completion widgets in my own scripts. At one point I'd like have access to the result of every completion that is active in zsh, or rather I want to obtain the final result of the completion chain.

function foo() {

  # call zsh completion
  comp_results=_main_complete_() # won't work like this, can only be called from within a completion function

  if [[ "$#comp_results" -gt "0" ]]; then
    # do something
    echo "results:"
    # do something else
    echo "no results"

I already thought of creating a custom completion function that just calls _main_complete (or whichever function is appropriate) and stores the results in an exported variable to make it accessible to other functions. However, this would also imply calling the custom completion function somehow - and I don't know how. Is it even possible to get "intermediate" completion results without performing the actual completion?


I'm not sure why zsh doesn't provide this functionality... Luckily someone made a hack to use the completion system pragmatically/non-interactively: https://github.com/Valodim/zsh-capture-completion

$ capture.zsh 'man gr'

This only give access to the finished completion list. Ie. not the internal zsh objects (descriptions, etc.)

(A similar question: Is it possible to manually invoke a zsh completion function?)

  • 1
    If you wish to install this using zplug you can use: zplug Valodim/zsh-capture-completion, as:command, use:capture.zsh, rename-to:capture.zsh – Att Righ Apr 18 '17 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.