38

I have an alias: alias gco='git checkout'

How can I add tab completion as if I had typed git checkout?

For my alias g='git' I use compdef g='git'.

I've been trying to use compdef but I've not had any success.

3 Answers3

27

You don’t need to do anything, especially compdef g='git', it should work without any configuration. You would need something only if you used function in place of an alias.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • 2
    I just get file completion. I've notices oh-my-zsh uses compdef; https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/git/git.plugin.zsh – William Bettridge-Radford Jan 13 '13 at 19:46
  • 3
    @WilliamBettridge-Radford These people must have done something to disable default behavior or just doing unecessary job. Just do `zsh -fautoload -Uz compinitcompinitalias gco='git checkout'gco `. I see a list of refs in this case. Another test: `alias g='git'compdef g='hg'g `: `compdef` is simply ignored, I get completion for git and not mercurial and am needing `compdef git='hg'` to override the behavior (obviously for both alias and `git`). – ZyX Jan 13 '13 at 19:54
  • That worked, thank you kindly. I'm using a fork of dotfiles from github. I think I'll turn them all off then add them back in slowly. https://github.com/holman/dotfiles – William Bettridge-Radford Jan 13 '13 at 20:05
  • My problem was that I was in fact using functions. Thanks! – Ambrose Little Jul 29 '19 at 21:26
27

Disable this option and you're also good...

# don't expand aliases _before_ completion has finished
#   like: git comm-[tab]
# setopt complete_aliases
Matt Ryan
  • 1,717
  • 2
  • 20
  • 30
  • 10
    This did it for me. Just to make it clear, you want to comment out that line in this response, not make your `config.zsh` look like it. You should **not** be setting the complete_aliases option if you want to have completion for aliases. – Matt Dodge Jun 22 '16 at 21:55
7

For me adding both of complete_aliases and compdef is necessary to get below snippet work:

alias docker-compose-dev='docker-compose -f docker-compose.yml -f docker-compose-dev.yml'

compdef docker-compose-dev='docker-compose'
setopt complete_aliases
ypresto
  • 975
  • 1
  • 13
  • 23