2.4.7. Tab completion#
Like many shells, bash has a useful built-in function called Tab completion.
By pressing the Tab-key while typing a command, you can make bash inspect your incomplete command and help you complete it.
One important use case of Tab completion is to complete (long) filenames for you:
Once you have typed part of a file or directory name that exists on the file system, and press the Tab key, the shell will complete the path for you.
If there is more than one file or directory with a name that starts with the character(s) you already types,
bashwill complete the name up to the ambiguous part. Pressing Tab again will show you a list of all the matches.
For example, instead of typing:
ls verylongdirectoryname
You can simply type
ls ve
and press Tab before pressing Enter (so, ls veTab).
If there are no other files or directories with names that start with ve,
bash will immediately know how to complete the name for you.
Exercise 2.45
The home directory here contains nested directory verylongdirectoryname/helloworld/anotherdirectory/.
Use cd to navigate all the way into anotherdirectory, but use Tab-complete every one of the three names.
So, type cd (including space) and press Tab three times, instead of typing them out by hand.
{
"filesystem": {
"/home/student/verylongdirectoryname/helloworld/anotherdirectory/": null
},
"checks": [
{ "type": "tabCompletion", "result": "verylongdirectoryname/", "kind": "path",
"desc": "should Tab-complete verylongdirectoryname" },
{ "type": "tabCompletion", "result": "verylongdirectoryname/helloworld/", "kind": "path",
"desc": "should Tab-complete helloworld" },
{ "type": "tabCompletion", "result": "verylongdirectoryname/helloworld/anotherdirectory/", "kind": "path",
"desc": "should Tab-complete anotherdirectory" },
{ "type": "commandEvent", "eventType": "builtin.cd",
"match": { "absPath": "/home/student/verylongdirectoryname/helloworld/anotherdirectory" },
"desc": "should end up inside anotherdirectory/" }
]
}
Exercise 2.46
This time, the home directory contains two directories, verylongdirectoryA/ and verylongdirectoryB/.
Type ls v and press Tab again, and bash will complete the unambiguous part verylongdirectory.
Press Tab again to lists both options instead of completing anything.
Continue typing to pick one, A, and press Tab again to complete it, and press Enter to run the command.
{
"filesystem": {
"/home/student/verylongdirectoryA/README.txt": "file in verylongdirectoryA",
"/home/student/verylongdirectoryB/README.txt": "file in verylongdirectoryB"
},
"checks": [
{ "type": "tabCompletion", "result": "verylongdirectory", "kind": "path",
"desc": "should Tab-complete at the ambiguous prefix verylongdirectory, listing both options" },
{ "type": "tabCompletion", "result": "verylongdirectoryA/", "kind": "path",
"desc": "should then Tab-complete the rest of verylongdirectoryA/" },
{ "type": "commandEvent", "eventType": "coreutil.ls",
"match": { "absPaths": ["/home/student/verylongdirectoryA"] },
"desc": "should run ls on verylongdirectoryA/" }
]
}
Note
Why is this beneficial, and should make Tab completion part of your muzzle memory?
Obviously, it reduces the amount of typing you need to do, making you work faster in the shell.
But it also reduces the risk of you mistyping long names. You make
bashdo more of the error prone work.It is even a useful mechanism to confirm that a filename you are typing actually exists, before running a command. If the Tab completion doesnât complete the filename, then maybe you are working in the wrong directory, or something else happened to the file. Without Tab completion, you would only find out after you tried to execute the command, and received an error.
With the ability to press Tab twice to see a list of all available names, this provides you a mechanism to quickly see the directory contents while you are completing a command.
Tab completion is not only useful for completing file and directory names. If you start typing the name of a program at the start of a command, and press Tab, it will try to complete the command based on the known commands.
Exercise 2.47
Type just t and press Tab to see every command starting with t. Then continue typing tem and press Tab again to complete it to tempdir (a simple command which just prints the path to a temporary directory, like /tmp), and press Enter to run it.
{
"checks": [
{ "type": "tabCompletion", "result": "t", "kind": "command",
"desc": "should Tab-complete at the ambiguous prefix t, listing every command starting with t" },
{ "type": "tabCompletion", "result": "tempdir", "kind": "command",
"desc": "should then Tab-complete the rest of tempdir" },
{ "type": "commandEvent", "eventType": "coreutil.tempdir",
"desc": "should run tempdir" }
]
}
It even knows how to complete the options of many programs!
While we canât demonstrate this in the bash emulator embedded in this manual, you can test this on your own computer.
Exercise 2.48
The git command has several subcommands, such as git clone and git commit.
Git provides support for bashâs Tab completion, allow you to complete the subcommand after git using Tab.
On your Linux computer with git installed, open a shell and type git (including the space, but no Enter).
Now press Tab twice, so that bash will show you all the subcommand options that can follow git.
Continue by typing a single c, and press Tab twice again (so, after git c)
Bash should now show you only the subcommands that start with c.
Inspect the list bash shows you.
Which of the following are git subcommands that start with c?
git cache
git call
git check-out
git checkin
git checkout
git cherry-pick
git clean
git coffee
git commit
git config
git control