Skip to main content

Ubuntu Tab Completion

Tab completion is one of the most powerful time-saving features in the Ubuntu terminal. This feature allows you to partially type commands, filenames, or directory paths and then press the Tab key to automatically complete them.

Introduction

When you're working in the terminal, typing long commands or navigating through deeply nested directories can be tedious and error-prone. Tab completion addresses this challenge by:

  • Reducing the amount of typing required
  • Preventing typos and syntax errors
  • Helping you discover available commands and options
  • Speeding up your workflow significantly

Whether you're a beginner just starting with the terminal or an experienced user looking to improve efficiency, mastering tab completion will dramatically enhance your Ubuntu experience.

Basic Tab Completion

Command Completion

Let's start with the most basic form of tab completion:

  1. Open your terminal (Ctrl+Alt+T)
  2. Begin typing a command, for example, type fir and then press the Tab key
$ fir[TAB]

Output:

firefox   

If there's only one possible completion (like firefox), the terminal will automatically complete the command for you.

If there are multiple possibilities, pressing Tab once will show no visible change. Pressing Tab twice will display all the possible completions:

$ fi[TAB][TAB]
find finger firefox firejail firewall-cmd

You can then type an additional character or two to narrow down the options and press Tab again to complete.

File and Directory Completion

Tab completion works for files and directories as well:

$ cd Do[TAB]

This will complete to:

$ cd Documents/

For directories with multiple possible matches:

$ cd D[TAB][TAB]
Desktop/ Documents/ Downloads/

Path Completion

Tab completion works at any point in a path:

$ ls /etc/net[TAB]

This will complete to:

$ ls /etc/network/

You can continue adding to the path and using tab completion at each level:

$ ls /etc/network/if-[TAB][TAB]
if-down.d/ if-post-down.d/ if-pre-up.d/ if-up.d/

Advanced Tab Completion

Partial Completion

If you're unsure of the exact name but remember part of it, you can use partial completion:

$ ls *conf[TAB]

This will complete or list all files ending with "conf" in the current directory.

Command Options Completion

Many commands support tab completion for their options:

$ apt --[TAB][TAB]

Output:

--admin-pin                  --install-suggests
--allow-change-held-packages --key
--allow-downgrades --list-cleanup
--allow-insecure-repositories --no-download
...and many more options

Variable Completion

You can even use tab completion with environment variables:

$ echo $HO[TAB]

This completes to:

$ echo $HOME

Bash Completion Package

Ubuntu comes with a basic tab completion system, but you can enhance it by installing the bash-completion package:

$ sudo apt install bash-completion

After installation, you may need to restart your terminal or source your .bashrc file:

$ source ~/.bashrc

This package provides more sophisticated completion capabilities for many common commands, including:

  • Git commands and branches
  • SSH known hosts
  • Service names
  • Package names for apt commands
  • Docker commands and container names
  • And many more

Customizing Tab Completion

Case Sensitivity

By default, tab completion in Ubuntu is case-sensitive. You can make it case-insensitive by adding the following to your ~/.inputrc file:

set completion-ignore-case on

If the file doesn't exist, create it first:

$ touch ~/.inputrc
$ echo "set completion-ignore-case on" >> ~/.inputrc

You'll need to restart your terminal for the changes to take effect.

Displaying Matches Without Double Tab

If you prefer to see possible completions after pressing Tab just once instead of twice, add this to your ~/.inputrc:

set show-all-if-ambiguous on

Auto Listing Completions

For automatic listing of all possible completions, use:

set show-all-if-unmodified on

Practical Examples

Example 1: Installing Packages

When installing packages with apt:

$ sudo apt install python3-[TAB][TAB]
python3-aiohttp python3-gnupg python3-pip
python3-apt python3-gst-1.0 python3-pkg-resources
python3-aptdaemon python3-html5lib python3-plainbox
... and many more packages

Example 2: Git Commands

If you have Git and bash-completion installed:

$ git ch[TAB]

Completes to available commands starting with "ch":

check-attr      check-ignore    check-ref-format cherry          cherry-pick
checkout

Example 3: Managing Services

When working with systemd services:

$ sudo systemctl status net[TAB]

This will list or complete to available services starting with "net":

$ sudo systemctl status network-manager

Example 4: Finding Configuration Files

When looking for configuration files in /etc:

$ less /etc/ssh/ssh_[TAB]

Completes to:

$ less /etc/ssh/ssh_config

Tab Completion in Other Shells

While we've focused on Bash (the default shell in Ubuntu), other shells like Zsh and Fish offer even more powerful tab completion features:

Zsh

Zsh offers features like:

  • Fuzzy completion
  • Spelling correction
  • Path expansion
  • Inline completion suggestions

To install Zsh:

$ sudo apt install zsh

Fish

Fish provides:

  • Syntax highlighting
  • Autosuggestions as you type
  • More user-friendly completions

To install Fish:

$ sudo apt install fish

Troubleshooting

Tab Completion Not Working

If tab completion isn't working as expected:

  1. Check if bash-completion is installed:

    $ apt list --installed | grep bash-completion
  2. Ensure your .bashrc is properly sourcing the completion file:

    $ grep -r "bash_completion" ~/.bashrc
  3. If missing, add these lines to your .bashrc:

    if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
    fi
  4. Restart your terminal or source your .bashrc:

    $ source ~/.bashrc

Summary

Tab completion is an essential skill for anyone using the Ubuntu terminal. It offers:

  • Faster command execution
  • Reduced typing errors
  • Discovery of available commands and options
  • Enhanced productivity

Starting with basic command and filename completion and advancing to customizations and specialized completions, this feature transforms the terminal experience from tedious typing to efficient navigation and command execution.

Additional Resources

  • Try running man bash and search for "completion" (type /completion and press Enter) to read the official documentation
  • Practice tab completion with the exercises below to build muscle memory

Exercises

  1. Navigate to your home directory and explore different folders using tab completion
  2. Try installing a package using apt and use tab completion to discover available packages
  3. Create a deeply nested directory structure and practice navigating it using tab completion
  4. Experiment with different tab completion customizations in your .inputrc file
  5. Try running different system commands with --help option and then explore their options using tab completion


If you spot any mistakes on this website, please let me know at [email protected]. I’d greatly appreciate your feedback! :)