How to use auto completion in bash (Debian/Ubuntu)

A must have feature that you should immediately enable if you are a Linux user. I will give the system package to install for Debian based system. But you can easily find the corresponding for your distro by checking repology, as recommended in the official repo of bash-completion.

What is shell

If you want completion in your bash, you have probably already understood what i am talking about. But to make a short explanation;
"A shell is a type of computer program called a command-line interpreter that lets Linux and Unix users control their operating systems with command-line interfaces. Shells allow users to communicate efficiently and directly with their operating systems." 1

On Debian/Ubuntu based distros

sudo apt update
sudo apt install bash-completion

Now you installed bash-completion. But you need to tell your bash to use it.

Enable bash-completion

If you are on the latest version of Debian/Ubuntu, you probably already have the following line in your ~/.bashrc file. If not, you can add it to the end of the file. Or if it is there but commented, just comment it out.

~/.bashrc
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
elif [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi

Done.

Note: this will enable bash-completion only for the current user. If you want to enable it for all users, go to /etc/bash.bashrc, you will see commented out lines for bash-completion. Just uncomment them.

How to use

It can complete pretty much anything default in your shell.

  1. For example,
cd # now press TAB twice

and it will show you all the directories you can change.


  1. Lets say there is directory named mydir in your home directory.
cd myd # now press TAB

an it will complete it for you.

If there are more than one directory starting with myd, you need to press TAB again to see your options as in Step 1.

It has some flaws

Since it has to assume some commands to be GNU versions, it may cause you some unintended behaviour on various systems. Please refer to the official repo for Known Problems. But in Debian/Ubuntu, you shouldn't face with any problem as i know of so far. Instead you it will make your life a lot easier.

References

  1. https://www.datacamp.com/blog/what-is-shell
  2. https://github.com/scop/bash-completion