Is it possible to manually invoke a zsh completion function

autocompletezsh

If I am at a zsh prompt and I have the current "context" (partially completed command-line command)

% man gr_
        ^ here I have used _ to  represent the cursor 

If I then invoke the _complete_help function CtrlXh

It tells me the context is

% man gr_
tags in context :completion::complete:man::
    manuals  (_man)

If I am reading this output correctly – I can see there is a completer function _man that is capable of listing all the known man pages – which would be invoked if I hit the Tab key at this point.
My question is, can I call the _man completer function manually? rather than having _man being invoked by compsys (which itself is invoked by Tab)?

I have tried using the zle minibuffer <Esc>x , then type _man<Enter> but I think the mini-buffer is only capable of understanding zle commands, not executing general functions.

Best Answer

I found that yes its possible and usable for simple completion functions, but because, this particular function _man does some parsing and validation work, it generates an error when invoked outside of its expected context.

Here's what I did to bind the _man completion function to a key combination so it could be invoked manually.

Define a wrapper function that sets the required options.

_man_autonomous () {
  eval $_comp_setup
  _man "$@"
}

Register a new widget with the line editor:

zle -C complete-man expand-or-complete _man_autonomous

bind the widget to Ctrl+T

bindkey '^T' complete-man

There is an example of this technique using _files in the zsh FAQ.

Related Question