Bash autocompletion is a feature that helps users quickly complete commands, filenames, variables, and arguments by pressing the Tab key. This reduces errors and speeds up command-line work.
- When you start typing a command and press Tab, Bash looks for matching commands or arguments.
- If there's only one match, Bash completes it automatically.
- If multiple matches exist, Bash shows a list of possibilities.
Bash autocompletion relies on:
bash-completionpackage – Provides advanced completions for many commands.- Completion scripts – Some tools like
kubectl,git, anddockerprovide their own autocompletion scripts. - Built-in filename completion – Works by default without extra setup.
dpkg -l | grep bash-completionIf not installed, install it:
sudo apt install bash-completion -yEnsure your ~/.bashrc includes:
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fiThen, reload the shell:
source ~/.bashrcSome commands require additional setup.
sudo apt install git bash-completion -y
echo 'source /usr/share/bash-completion/completions/git' >> ~/.bashrc
source ~/.bashrcecho 'source <(kubectl completion bash)' >> ~/.bashrc
source ~/.bashrcecho 'source /usr/share/bash-completion/completions/docker' >> ~/.bashrc
source ~/.bashrcYou can define your own autocomplete scripts using complete command.
Example: Enable autocomplete for a custom command mycmd:
complete -W "start stop restart status" mycmdNow typing mycmd s[Tab] will suggest start, stop, status.
- Reload Bash Configurations:
source ~/.bashrc
- Check if Completion is Enabled:
complete -p | grep kubectl
- Manually Load Completion for a Command:
source /usr/share/bash-completion/completions/git
Bash autocompletion enhances productivity by reducing typing effort and errors. Setting up bash-completion and enabling custom completions for frequently used tools like Git, Docker, and Kubernetes makes CLI work much easier.
Would you like a guide on writing advanced custom autocompletion scripts? 🚀