Let the Shell Type for You: Tab
Press Tab and the shell finishes a file or folder name for you, press it twice to see every name that fits, and let it type the backslashes that a name with spaces needs.
Novice · Unix · Linux · Updated
After this lesson you can
- Complete a file or folder name with Tab, and press Tab twice to list the names that fit
- Type a name with spaces in a command, with quotes, backslashes or Tab
Key takeaways
- Type the first few letters of a name and press Tab, and the shell finishes it.
- When several names fit, the first Tab finishes only the part they share. A second Tab lists them all, so you can type one more letter.
- A name with spaces needs quotes ("photos to print") or a backslash before each space. Tab types the backslashes for you.
- When Tab won't finish a name, it probably isn't there or is spelled differently, so you catch the typo before you press Enter.
Folder names get long: day-3-mountains, photos to print, a project called spring-semester-biology-lab. Type one letter wrong and the command fails. People who use the terminal every day hardly ever type a whole name. They type a few letters and press Tab, and the shell types the rest. It's faster, and it's more accurate than typing by hand.
The examples use the practice folder from Where Am I? pwd and Paths: cd ~/terminal-practice/trip.
One Tab finishes the name
Type cat pack (with no Enter yet), then press Tab. The shell looks for names in this folder that start with pack, finds only one, and finishes it, adding a space so you can keep typing:
cat packing-list.txt
Now press Enter. cat prints a file's text on the screen (Reading Files: cat and less, in the next chapter, covers it):
cat packing-list.txt
sunscreen
hiking boots
phone charger
Tab works for folders the same way, and for each folder in a longer path. Type cd day-1 and press Tab: it becomes cd day-1-beach/, with a / on the end because it's a folder, ready for the next name inside it. This is called tab completion.
Two Tabs show the choices
Now type cd day- and press Tab. Three folders start with day-, so the shell can't choose, and nothing happens (your terminal may beep). Press Tab a second time, and it lists every name that fits, then gives you back your line, unchanged:
$ cd day-
day-1-beach/ day-2-city/ day-3-mountains/
$ cd day-
Type one more letter, 2, and press Tab again: the line becomes cd day-2-city/. When the names that fit share more letters than you've typed, the first Tab fills in that shared part. ls d and Tab becomes ls day-, and a second Tab shows the choices.
In zsh, the shell on a Mac, one Tab already shows the choices, and pressing Tab again steps through them.
Names with spaces
The shell splits every command at its spaces: the first word is the command, and each word after it is a separate thing to give it. So a folder name with spaces confuses it:
cd photos to print
bash: cd: too many arguments
cd was given three things, photos, to and print, and it can go to only one place. There are two ways to say "these spaces are part of one name". Put the whole name in quotes, or put a backslash, \ (read "backslash"), before each space:
cd "photos to print"
pwd
/home/sam/terminal-practice/trip/photos to print
cd photos\ to\ print gets to the same place. You rarely have to choose: type cd ph and press Tab, and the shell writes cd photos\ to\ print/ for you. This is also why so many people name their own folders with hyphens instead of spaces, like day-1-beach: nothing to quote.
Tab catches mistakes
When Tab won't finish a name, it's telling you something. Either several names fit (press Tab again to see them), or no name in that folder starts the way you typed. You may have a typo, or be in the wrong folder: pwd and ls will show which. Either way, you find out before you press Enter, not from an error afterward.
Try it in the terminal
Terminal Quest: The Lighthouse, goals 8 to 9: played right on the lesson page.
Your turn: search the shore for something hidden, then climb to the top of the tower in one cd, letting Tab type each long name.
Check yourself
2 questions to try on the lesson page.
So what
Tab makes long names short to type, finds names you only half remember, and catches typos as you go. Use it on every name, and the terminal gets faster than clicking.