I use zsh with the “new” completion system (compinit). Sometimes I want to restrict completion to a subset of what is possible, to cut down on ambiguity. This is a user choice, not a configuration setting: I want to hit a different key instead of Tab to get a different set of completions. I don't want to configure completions differently for that command. Tab should still do its normal thing.

In some cases, there are workarounds. For example, I have a key bound to _bash_complete-word, which in most cases completes files. But sometimes I want a less common focus. For example:

  • Complete only host names after scp, not local file names.
  • Complete only remote names after git checkout, not branch names.

How can I bind a key that will complete remote names after git checkout, but not branch names? More generally, how can I bind a key to complete something different from what would normally be completed in the current context?

1 Answer 1


The snippet below causes Meta+r to complete git remote names when on a git command line, and host names elsewhere.

_complete-remotes () {
  case $words[1] in
    git) load_helper_functions _git; __git_remotes;;
    *) _hosts;;
compdef -k _complete-remotes complete-word '^[r'
  • See Can I use a helper function from a ZSH completion file in another for load_helper_functions. If you remove it from the code above, it will only work if completion for git has been performed at least once before in the current zsh session.
  • $words[1] is the first word of the command-with-arguments that is to be completed.
  • complete -k … registers the function _complete-remotes as a completion widget (which allows it to run completion builtins) and binds it to Meta+r.

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.