r/bash•
bubbleguuum

programmable completion on expanded aliases not working (shopt -s progcomp_alias)

In Bash 5, there is a progcomp_alias shopt parameter. From man shopt:

If set, and programmable completion is enabled, bash treats a command name that doesn't have any completions as a possible alias and attempts alias expansion. If it has an alias, bash attempts programmable completion using the command word resulting from the expanded alias.

Let's take an example with systemctl

If I type systemctl <TAB><TAB> I get programmable completion:

add-requires            enable                  is-system-running       preset                  show
add-wants               exit                    kexec                   preset-all              show-environment
cancel                  force-reload            kill                    reboot                  start
cat                     get-default             link                    reenable                status
condreload              halt                    list-dependencies       reload                  stop
condrestart             help                    list-jobs               reload-or-restart       suspend
condstop                hibernate               list-machines           rescue                  suspend-then-hibernate
daemon-reexec           hybrid-sleep            list-sockets            reset-failed            switch-root
daemon-reload           import-environment      list-timers             restart                 try-reload-or-restart
default                 is-active               list-unit-files         revert                  try-restart
disable                 is-enabled              list-units              set-default             unmask
edit                    is-failed               mask                    set-environment         unset-environment
emergency               isolate                 poweroff                set-property

Now if I create an alias with alias s=systemctl and turn on progcomp_alias with shopt -s progcomp_alias I would expect that when I type s <TAB><TAB> to see the programmable completion above. Yet it doesn't work and propose only regular file completion. Am I using the progcomp_alias option wrong or not understanding what it does ?

Posted Jan 19, 2020, 1:57 PM UTC · edited

85
bikes-n-math•

a command name that doesn't have any completions.

Pretty sure s has pleanty of completions.

1
bubbleguuumOP•

I suppose the doc means if the command is an existing complete command (which s is not). You can alias cvsjkdnfkjsdf=systemctl and it will not work either and cvsjkdnfkjsdf has no completion...

1
obiwan90•

I would have expected it to work like you did, and I have a use case myself that I'd love to work when using progcomp_alias, but I didn't get it to work either.

A little digging surfaced a mailing list thread - maybe complete -D is interfering?

Specifically, if you have the bash-completion package installed, it looks like the dynamic completion loader is set to

complete -F _completion_loader -D

potentially resulting in progcomp_alias not having any effect.

1
bubbleguuumOP•

Yes, that's it definitely. A bit of a bummer that there seems to be no way for progcomp_alias to have precedence over complete -D. Unless there is a workaround ?

1
bubbleguuumOP•

Solution (to put in your ~/.bash) that clear default completion handler and manually load individual completion that you use (edit the commands array)

 

# remove default completion handler
complete -r -D 2> /dev/null

# load completions that we use

completions_dir=/usr/share/bash-completion/completions

commands=(systemctl ssh find virsh)

for command in ${commands[*]} 
do
    . $completions_dir/$command
done

# turn on alias programmable completion

shopt -s progcomp_alias
1