How can I add Bash tab completion to aliases for pacman?

0
1
Asked By MellowCedar42 On

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?

2 Answers

Answered By QuartzMango7 On

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.

BrightOtter19 -

Bash completion can work with aliases, although shell functions are often more flexible if you later need custom argument handling.

Answered By CopperLynx28 On

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.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.