Keyboard Trick to Auto Complete Commands in Linux Using TAB


Here is a keyboard trick you can do by using TAB in Linux. All the Unix commands and the contents of the current directory are known to BASH (the Bourne Again SHell ), so you don’t need to type all the characters of a command or path. Just type in the first few and hit [Tab] and this will auto- complete the command for you.

If you hear a beep, it doesn’t mean an error: there’s just more than one option with the same starting characters. Press [Tab] again to see a list of all the commands that begin with the characters you keyed in. The same applies to auto-completing a filename or path.

Say you want to switch from /root to /usr/local/mystuff/videos. This is how you use [Tab] in this case:

$cd /us [Tab] auto-completes as: /usr. Now continue without pressing [Enter]:
$cd /usr/loc [Tab] This auto-completes as: /usr/local.
Now, $cd /usr/local/my [Tab] auto-completes as /usr/local/my\ stuff/ .

Do not press [Enter] yet! We’ve deliberately included a space in the final folder name so as to demonstrate the importance or the usefulness of the [Tab] key when dealing with folder names containing
spaces. $cd /usr/local/my\ stuff/[Tab]

As shown above, the space in a folder or file name is preceded with a backslash; it informs the shell of the space. Otherwise, the shell will consider it as two separate files, “my” and “stuff.”

If “my stuff” only contains one folder as in our case, “videos” then [Tab] will complete the path even if you don’t type in any characters, since it is the obvious choice. The desired path using the auto-complete technique will appear as $cd /usr/local/my\stuff/videos

You can now press [Enter] to change to that directory. This technique can be applied to almost everything except with the “man” command.

Comments

Leave a Reply