Own local completions may be defined in ~/.local/share/bash-completion/completions
as explained in the bash-completion FAQs
A simple approach is thus to create this directory (if not existing) and put a file named mycommand
(with alias mycommand='realcommand'
as the alias definition) and to just a) source the original command's completion and b) refer to the function in complete
, e.g.:
$cat ~/.local/share/bash-completion/completions/mycommand source /usr/share/bash-completion/completions/realcommand complete -F _realcommand mycommand
If the function is called _realcommand
. Use completion on realcommand
and then complete -p | grep realcommand
to see how the function is actually called.
If the alias is meant to be system-wide, one may as well place the file in /usr/share/bash-completion/completions/
directly.
Limitations
Needs manually adding the entry for each alias, not automated.
Fails if the completion function refers to the command itself:
It worked nicely for e.g. alias userctl='systemctl --user'
, it (partly) failed for alias mylsblk=lsblk
when tab-completing anything other than mylsblk -<TAB>
. This is due to the function _lsblk_module
referring to the command name (via $1
) in the completion script and this fails as mylsblk
cannot be found as command. It worked, when I replaced $1
with lsblk
in the completion script of lsblk
(not recommended, done for testing purposes only).
In this case, the local completion script has to be slightly adapted so that the first argument remains the command name:
source /usr/share/bash-completion/completions/lsblk _lsblk_dummy() { _lsblk_module lsblk ; } complete -F _lsblk_dummy mylsblk
which might as well be seen as general approach.