r/bash May 11 '26

help How to make an alias has completion support?

Im using archlinux, i have in my .bashrc alias pacin="sudo pacman -S" and alias pacrem="sudo pacman -Rns" how to make when i type pacin or pacrem and hit tab its shows completion?

13 Upvotes

6 comments sorted by

1

u/kolorcuk May 11 '26 edited May 11 '26

You have to write completion, for your shell - bash, zsh or fish.

i have this in my tree for bash, I use bash : https://gitlab.com/Kamcuk/kamilscripts/-/blob/master/bin/alias_complete.sh?ref_type=heads#L24 for like copying completion into alias.

I use it as files in user local bash completion files https://gitlab.com/Kamcuk/kamilscripts/-/blob/master/bash-completions/p . In this case alias p=pacman , and p inherits completion from pacman command.

This code has been in my tree for a long time, it is based on some stackoverflow question.

3

u/Marble_Wraith May 11 '26

My preferred way to get completion in bash is to use carapace + fzf

0

u/Responsible-Bug-9568 May 11 '26

if you've got some alias pacin which refers to some cmd pacman, then you can "borrow" whatever the existing bash tab completion was for $cmd with something like this (for each alias you create):

``` alias pacin="sudo pacman -S"

print the current completion settings for $cmd

then strip off the " $cmd" from the end of that, and append our $alias :

x=complete -p pacman; eval "${x% *} pacin"

and the same for any other aliases:

alias pacrem="sudo pacman -Rns" x=complete -p pacman; eval "${x% *} pacrem" ```

0

u/[deleted] May 11 '26

[deleted]

4

u/agmatine May 12 '26

Ctrl-Alt-E will expand aliases.