How can I add Bash tab completion to aliases for pacman?
Asked by
MellowCedar42
I'm using Arch Linux and Bash. In my .bashrc, I have aliases like alias pacin="sudo pacman -S" and alias pacrem="sudo pacman -Rns". The commands work, but when I type pacin or pacrem and press Tab, I don't get pacman's package-name completion. How can I make these aliases reuse pacman's existing Bash completion?
Answers
For a larger number of aliases, a small reusable completion helper can copy the completion settings from the original command to each alias. Youโll need to implement or install such a helper for Bash, and then apply it to pacman, pacin, and pacrem. Tools such as carapace combined with a fuzzy finder are another approach if you want a more extensive completion system, but theyโre not necessary for simply sharing pacmanโs existing completion.
Reply to this answer
You can reuse pacmanโs existing completion definition and attach it to each alias. For example:
alias pacin="sudo pacman -S"
alias pacrem="sudo pacman -Rns"
x=$(complete -p pacman)
eval "${x% *} pacin"
eval "${x% *} pacrem"
The first command retrieves pacmanโs current completion configuration, removes the original command name, and applies that configuration to the aliases. Put this after the relevant completion setup in your .bashrc, then start a new shell or reload the file.
Reply to this answer
Add your answer
Answers are reviewed before publication.
BrightOtter19: Bash completion can work with aliases, although shell functions are often more flexible if you later need custom argument handling.