zshzsh-completionzsh-alias

Zsh completion for global aliases


Is there a way to get tab completion for global aliases in zsh? Defined as:

% alias -g zshplugins=~/.zshplugins

% nvim zshpl[tab] would not tab complete.

I use global aliases mainly to not have to enter the path to a file nor restrict myself to a single command (e.g., alias zshplugin="nvim ~/.zshplugins")

I understand that these are also meant to be used inside arbitrary one-liners (a global alias for | grep -i, for instance) and don't make sense to suggest on every tab stroke, but If there's some workaround to include these in directory/file completion, that would be great.


Solution

  • You should use the shell variable but not global alias.

    But if you want, add following code to your zshrc

    _complete_alias() {
        [[ -n $PREFIX ]] && compadd -- ${(M)${(k)galiases}:#$PREFIX*}
        return 1
    }
    zstyle ':completion:*' completer _complete_alias _complete _ignored