simohamed
simohamed

Reputation: 81

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.

Upvotes: 0

Views: 918

Answers (2)

Aloxaf
Aloxaf

Reputation: 266

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

Upvotes: 2

user1934428
user1934428

Reputation: 22225

Would zshpl[tab] complete at the beginning of the line (leaving out nvim)? This what I should work. An alias is not meant to be used as a shortcut for a file name. Shell variables can be used for the latter (and there is a completion on them too). I suggest that you define

zshplugins=~/.zshplugins

and then do

nvim $zshpl[tab]

Upvotes: 0

Related Questions