Why Tab stays silent
Tab completion in Bash is a function bound to a command name. For systemctl, that function is _systemctl, loaded
by the bash-completion package on the first Tab after systemctl. Type systemctl sta, press Tab twice, and you get
start and status.
The name sc has no completion function of its own. Bash doesn’t expand aliases before running completion, so it falls
back to filenames in the current directory. sc sta + Tab gives nothing; sc + Tab lists file-one, file-two.
Single-command alias: two lines
When an alias wraps a single command with no arguments, bind that command’s completion function to it:
# ~/.bash_completion — bash-completion reads it last
_completion_loader systemctl
complete -F _systemctl sc
_completion_loader systemctl loads systemctl’s completion immediately instead of waiting for the first Tab. Without
it, _systemctl doesn’t exist at login yet. Find the function name for any other command with complete -p:
complete -p systemctl
# complete -F _systemctl systemctl
Put these lines in ~/.bash_completion, not ~/.bash_aliases. Ubuntu’s .bashrc sources ~/.bash_aliases before
loading bash-completion, when those functions aren’t defined yet. bash-completion sources ~/.bash_completion itself
at the end of its own initialization.
Alias with arguments: complete-alias
For dc='docker compose', binding _docker falls short. _docker expects top-level docker subcommands: dc comp
- Tab turns into
dc compose, and it never suggestsdownorlogs. It expectsdocker composeon the line.
That’s what complete-alias handles — a Bash script on GitHub under the GPL license. Before completing, it expands the alias on the command line and calls the real command’s completion. Download a pinned tag rather than a branch so the file doesn’t change behind your back, and read it before sourcing:
mkdir -p ~/.local/share
curl -fsSL -o ~/.local/share/complete_alias \
https://raw.githubusercontent.com/cykerway/complete-alias/1.18.0/complete_alias
less ~/.local/share/complete_alias
Version 1.18.0 is around 1060 lines and makes no network calls. Source it in ~/.bash_completion and list your
aliases:
# ~/.bash_completion
. ~/.local/share/complete_alias
complete -F _complete_alias sc dc
Now dc do + Tab completes down, and dc logs --ta completes --tail. It handles single-command aliases too, so
you can include sc here and drop the two lines from the previous section.
If it doesn’t work
Login prints _completion_loader: command not found, and Tab on sc says bash: completion: function … not found
- Cause
- The completion lines are in
~/.bash_aliases. Ubuntu’s.bashrcreads that file before loading bash-completion, so those functions don’t exist yet. - Fix
- Move the lines into
~/.bash_completion: bash-completion sources it at the end of its own startup.
After complete -F _docker dc, Tab on dc comp completes compose, and dc ima completes image
- Cause
_dockercompletes subcommands ofdockeritself. It has no ideadcalready includescompose.- Fix
- For an alias with arguments, use complete-alias:
complete -F _complete_alias dc.
complete -p systemctl prints no completion specification
- Cause
- bash-completion loads a command’s completion on first Tab press, so it isn’t registered before then.
- Fix
- Run
_completion_loader systemctl, thencomplete -p systemctlagain.
How to check
Start a new shell or run source ~/.bash_completion:
complete -p sc dc
# complete -F _complete_alias sc
# complete -F _complete_alias dc
Type sc sta and press Tab twice — you should see start and status.