Skip to content

Bash

Customize completion for aliases

Idea: Overwrite COMP_WORDS and COMP_CWORD then call the original completion function.

alias userctl='systemctl --user'
if command -v _systemctl &>/dev/null; then
  _userctl() {
    COMP_WORDS=("systemctl" "--user" "${COMP_WORDS[@]:1}")
    COMP_CWORD=$((COMP_CWORD + 1))
    _systemctl "$@"
  }

  complete -F _userctl userctl
fi