Working in the terminal is a core part of managing Ubuntu Server, and enabling autocompletion can significantly boost your productivity. This feature allows you to hit the Tab key to auto-complete commands, paths, package names, and more—minimizing typos and saving time. Here’s how to properly enable autocompletion on Ubuntu Server 24.04 LTS.
1. Check if bash-completion is Already Installed
Before installing anything, check if the required package is already present:
apt list --installed | grep bash-completionCode language: PHP (php)
If you see a line with bash-completion, it’s already installed. Otherwise, move on to the next step.
2. Install bash-completion
If it’s missing, install it using:
sudo apt update && sudo apt install -y bash-completion
3. Enable bash-completion in Your Shell Session
Once installed, make sure Bash loads it automatically. Open your user’s .bashrc file:
nano ~/.bashrc
Add or uncomment the following block if it’s not already there:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
Save the file and reload the shell settings:
source ~/.bashrc
4. Enable Autocompletion for sudo Commands
By default, commands run with sudo don’t benefit from autocompletion. To enable it system-wide, edit:
sudo nano /etc/bash.bashrc
Add this line at the end:
complete -cf sudo
Apply the changes:
source /etc/bash.bashrc
5. Troubleshooting Autocompletion Issues
5.1 Ensure Bash Is Using bash-completion
Check if the variable is set:
echo $BASH_COMPLETION_COMPAT_DIRCode language: PHP (php)
If it’s empty, manually source the script:
source /usr/share/bash-completion/bash_completion
5.2 Reinstall the Package
If the script isn’t present:
sudo apt reinstall bash-completion
5.3 Confirm You’re Using Bash
If you’ve switched to another shell, check your current shell:
echo $SHELLCode language: PHP (php)
If it doesn’t show /bin/bash, set it back:
chsh -s /bin/bash
exec bash
6. Optional: Enhance Autocompletion with Extra Tools
You can further improve your command-line experience by installing tools like:
- autojump – Fast directory navigation
- fzf – Interactive file and command search
- zsh – A powerful alternative shell with advanced autocompletion
Install them with:
sudo apt install -y autojump fzf zsh
To switch to zsh:
chsh -s $(which zsh)Code language: JavaScript (javascript)
7. Test That It Works
Try typing a partial command and press Tab twice:
sudo apt in<TAB><TAB>Code language: HTML, XML (xml)
You should see suggestions like install, info, etc.
Final Thoughts
Enabling autocompletion in Ubuntu Server 24.04 is a simple tweak that can greatly improve your efficiency and reduce frustration in the terminal. With bash-completion enabled and some optional tools, you can create a more streamlined and powerful shell experience tailored to your workflow.
