4

I have a bunch of aliases like this

alias -g foo=cmd1
alias -g faz=cmd2
alias -g fam=cmd3

At the shell (I'm using zsh) I want to be able to type

echo fa<TAB>

and be prompted for faz or fam

complete_aliases option doesn't seem right (and didn't work) and compdef is for completion for cmd* not to complete the alias name itself.

If I use the aliases at the beginning of a line they do offer tab completion. It's only when it's used somewhere else in the line.

Functions like oh-my-zsh's globalias works for expanding the alias after I type the full name and push space but I'm looking for something that will expand the alias if I don't remember the full name of it.

1 Answer 1

1

Add this to your ~/.zshrc file:

autoload -Uz compinit
compinit
# The code below should come _after_ initializing the completion system.

# Autoload the `galiases` table.
zmodload -Fa zsh/parameter p:galiases

# Whenever a completion is attempted, first run `_galiases`.
compdef _galiases -first-

_galiases() {
  # Add the completions to the `aliases` group.
  local expl
  _description aliases expl 'alias'

  # Add the keys from `galiases` as the actual completions.
  compadd "$expl[@]" -Q -k galiases
}

(Note that you cannot define multiple completion functions as -first-. If you have other completion functions that should be called first, then you should define an umbrella function that calls all of them.)

Documentation:

2
  • You should put the zmodload line outside the function. It only needs to be called once.
    – Adam Katz
    Commented Mar 17, 2021 at 14:31
  • Thanks. I updated my answer. Commented Mar 17, 2021 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .