Using zsh autocompletion for alias

6,906

Solution 1

I've had the same issue. You should check whether the option completealiases is set. It prevents aliases from being internally substituted before completion is attempted. In my case, removing setopt completealiases from my .zshrc resolved the issue. You can try unsetopt completealiases if oh-my-zsh sets it.

Solution 2

If you open the file which contains the autoloaded function defintion for git completion:
On my ubuntu system, its:

/usr/share/zsh/functions/Completion/Unix/_git

at the top of the file you will see the #compdef declaration

#compdef git git-cvsserver git-receive-pack git-upload-archive git-upload-pack git-shell gitk tig

you can try adding your aliases to the #compdef definition, e.g.

#compdef git git-cvsserver git-receive-pack [...] tig gch grb
                                                      ^^^ ^^^

Then starting a new shell to get that new directive loaded.
Then try invoking completion.

What is /usr/share/zsh/functions/Completion/Unix/_git?

Its the file that contains the autoloaded "function" defining the completion behaviour for the all the commands defined in the #compdef directive, in this case the git family of commands, it lives in fpath and is autoloaded by compinit when the completion system is initialized.

What is #compdef ?

from man zshcompsys

#compdef name ... [ -{p|P} pattern ... [ -N name ... ] ]
The file will be made autoloadable and the function defined in it will be called when completing names, each of which is either the name of a command whose arguments are to be completed or one of a number of special contexts in the form -context- described below.

Share:
6,906

Related videos on Youtube

Patrick Artounian
Author by

Patrick Artounian

Updated on September 18, 2022

Comments

  • Patrick Artounian
    Patrick Artounian over 1 year

    I have created a few aliases for git in zsh, for example gch = git checkout, grb = git rebase --committer-date-is-author-date and some more complex useful zsh functions for git commands. But how can I allow these aliases to use zsh git autocompletion?

  • Patrick Artounian
    Patrick Artounian over 7 years
    Yes, I am oh-my-zsh and I also already have that in the zshrc.
  • Patrick Artounian
    Patrick Artounian over 7 years
    Is there a nicer way to make this happen so it can fit into my dotfiles?
  • the_velour_fog
    the_velour_fog over 7 years
    yes there are other ways to script this out but did this work for you? I guess there is no point bothering with improving on this solution if it fundamentally didnt work in the first place.
  • Patrick Artounian
    Patrick Artounian over 7 years
    Just tried it and no dice. I apologize for rushing into things.
  • the_velour_fog
    the_velour_fog over 7 years
    @patrick oh ok sorry. it works for me with functions, however I dont use aliases anymore so I couldnt test on my system
  • Sohail Ahmed
    Sohail Ahmed over 2 years
    This also fixed git-together, which works by aliasing git to its own command.