Sometimes a command doesn't have a completion installed—or syntactically could accept a whole bunch of types of arguments, rendering completion next to useless. For instance, "git checkout" can take the name of any git-tracked file or the name of any ref or other tree-ish. It can also take some option flags. That's a lot of possible completions.

Knowing that I want to complete—say—a branch name, is there a way to bind a key to complete against or display a menu of branch names? (And likewise for any category of completion.)

That seems possible and useful, but I haven't seen it done.


Yes, this can be done. I originally got the following from one of the zsh mailing lists, but I don't recall who posted it:

zle -C complete-file complete-word _generic
zstyle ':completion:complete-file::::' completer _files
bindkey '^x^f' complete-file

This could be easily adapted to use a different completion function rather than _files.

The first line defines a new completion widget named complete-file which behaves like the builtin complete-word widget and uses the _generic function to generate the list of completions.

The second line then configures it so that when that custom widget is used the completer will only use _files to get the list of completions.

Then the third line binds the new widget to a key sequence.

| improve this answer | |

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.