'zsh alias completion (trailing space) breaks with functions

When you create an alias with a trailing space like alias sudo='sudo ', it instructs zsh or bash to traverse the next argument for an alias, so if you also have alias aup='apt update', you could say sudo aup and it'll run apt update as root.

This does not, however, work when you have a function. For example:

_installer_helper() {
  local RETVAL
  command "$@"
  RETVAL="$?"
  hash -r
  return "$RETVAL"
}

alias apt='_installer_helper apt'

# (zsh only) this ensures apt subcommands still complete correctly
setopt complete_aliases

alias sudo='sudo '

Not what I want:

$ sudo apt update
sudo: _installer_helper: command not found
$ 

How do I get this to work properly in both bash and zsh? (Yes, the setopt command is only run by zsh.)



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source