Bash completion for aliases

seashells

Bash completion is a wonderful feature that allows you quickly compose complex shell commands. Add muscle memory to the mix, and one-liners will appear on the screen as if you were typing with 20 fingers. Add Bash aliases on top of that, and… you’re back to square one.

Sometimes aliases break the completion functionality, and that can be very annoying. Thankfully, there’s a solution in the form of a Bash script ingeniously called complete_alias.

Setup

Assuming that you already have the bash-completion package installed (naming may vary on certain Linux distributions), the installation of complete_alias is very simple. Clone complete_alias Git repository:

git clone https://github.com/cykerway/complete-alias.git ~/.complete_alias

Source the complete_alias script in ~/.bashrc (or .bash_completion, if you have one):

1
2
3
if [ -f ~/.complete_alias/complete_alias ]; then
    . ~/.complete_alias/complete_alias
fi

Add definitions for each alias that has broken Bash completion (again in ~/.bashrc). For example:

1
2
3
4
5
alias srsync="rsync --rsync-path 'sudo rsync'"
alias ll='ls -l'

complete -F _complete_alias srsync
complete -F _complete_alias ll

Alternatively, you can complete all your aliases with the following one-liner (after you’ve declared all your aliases):

1
complete -F _complete_alias "${!BASH_ALIASES[@]}"

Start a new shell session or source ~/.bashrc in your current session and that’s it.