LearnLinux FoundationsNavigation

Tab Completion

Tab - a hands-on Linux lab on a real virtual machine.

Press Tab to finish a folder name for you. Learn single-Tab completion, double-Tab to list choices, and tie-breaking on the recon-base filesystem, then run it for real in the Navigation mission lab.

Back on recon-base. You already know ls lists a folder, and a path is the address of a folder, like ~/recon-base/documents/. The ~ is the shortcut that means "my home folder."

Here is the problem nobody warns you about. Real folders have long names. Typing documents by hand, letter by letter, every single time, is slow, and one wrong key means the command fails.

There is a key on your keyboard that types the rest of the name for you. Most people never learn it. You are about to.

The black boxes in this lesson are a practice terminal. It is a safe sandbox that only checks the one command each step teaches, so you cannot break anything. You will run these moves for real on the module's Navigation mission machine at the end.

The key is Tab. It sits on the far left of your keyboard, above Caps Lock, marked with two arrows.

In the terminal, Tab does something magic. You type the first few letters of a name, press Tab, and the shell finishes the name for you. This is called tab completion.

The shell is the program reading what you type, the part that runs your commands. When you press Tab, the shell looks at what you started typing, checks the folder, and fills in the only name that fits.

Tab completion is the single biggest time-saver in the terminal. Engineers who have typed for twenty years still lean on it for every path. Build the habit now and you never look back.

Time to see it work. You want to list the documents folder inside recon-base.

Instead of typing the whole word documents, you type just enough to be unique, then let Tab do the rest.

In the practice terminal, type this exact, unfinished command:

ls ~/recon-base/doc

Now press the Tab key once. The shell finishes the name, and your line turns into ls ~/recon-base/documents/. Press Enter to run it.

The practice terminal cannot feel your Tab keypress, so type the finished command below. On the real machine at the end, the Tab key does the completing for you.

prompt: student@linuxcamp:~$ answer: ls ~/recon-base/documents/ output: after-action-report.txt equipment-list.txt mission-brief.txt roster-backup.txt roster.txt hint: The completed command is ls ~/recon-base/documents/ then Enter.

You typed doc, pressed one key, and the shell wrote out documents/ for you. Three letters instead of nine, and zero chance of a typo.

The trailing slash the shell added is a bonus signal: it means documents is a folder, not a file. Tab only added it because the completion was a directory.

Tab finished doc instantly because only one name in recon-base starts with those letters: documents. When the start of a name points at exactly one thing, the shell is certain, so it completes it.

The non-hidden names in ~/recon-base/ are:

broken-link comms documents intel latest-briefing logs ops

Only documents begins with doc, so doc plus Tab can mean nothing else. That is why it worked in one press.

The rule: Tab completes the moment what you typed matches exactly one name. Type too little and it matches several. That case is next, and it has its own trick.

Head into the ops folder. It holds the operation plans, and three of them start with the same letters: plan-alpha, plan-bravo, plan-charlie.

Type this unfinished command in the practice terminal:

ls ~/recon-base/ops/plan-

Press Tab once. Nothing completes. The shell cannot guess which plan you mean, because plan- matches all three. So it stays silent and waits.

Now press Tab a second time. The shell gives up guessing and instead lists every name that fits, so you can see your options:

plan-alpha  plan-bravo  plan-charlie

Remember this pattern. One Tab completes a sure thing. Two Tabs, pressed quickly, list the choices when the shell is unsure. Double-Tab is how you ask "what are my options?"

The double-Tab just showed you three plans. Now you pick one and let Tab carry you the rest of the way.

You want plan-alpha. Add one letter to break the tie: type a right after plan-, so your line reads ls ~/recon-base/ops/plan-a. Now plan-a matches only one name.

Press Tab once. The shell completes it to ls ~/recon-base/ops/plan-alpha/. Press Enter to run it.

ls ~/recon-base/ops/plan-alpha/

prompt: student@linuxcamp:~$ answer: ls ~/recon-base/ops/plan-alpha/ output: 'mission log.txt' objectives.txt timeline.txt hint: The completed command is ls ~/recon-base/ops/plan-alpha/ then Enter.

That is the full loop: too many matches, double-Tab to see them, one more letter to break the tie, then a single Tab to finish. You steered to exactly the folder you wanted and barely touched the keyboard.

Notice 'mission log.txt' printed inside quotes. That name has a space in it, and the quotes are ls showing you where the name really starts and ends. Spaces in names cause trouble, which is one more reason to let Tab type them for you instead of guessing.

Training wheels off. No completed command this time, you build it.

The radio logs live in the comms folder inside recon-base. List that folder, but earn it: after ls ~/recon-base/ you may type only one letter before pressing Tab.

Look back at the recon-base names and decide which letter can only mean comms. Before you press Enter, commit: how many names do you expect to print?

prompt: student@linuxcamp:~$ answer: ls ~/recon-base/comms/ output: radio-log-day1.txt radio-log-day2.txt radio-log-day3.txt radio-log-day4.txt radio-log-day5.txt radio-pipe hint: Only one name in recon-base starts with c. Type ls ~/recon-base/c, press Tab, then Enter.

One letter, c, was enough: no other name in recon-base starts with it, so a single Tab finished comms/ for you.

Six names printed: five radio logs and radio-pipe, a special kind of file you will meet in a later lesson.

Last challenge. This time you need the charlie plan inside ops.

You already know plan- matches three names, so a bare Tab will stall. Decide before you touch the keyboard: which single letter after plan- makes the match unique? Type the shortest prefix that works, press Tab, and run the completed command.

prompt: student@linuxcamp:~$ answer: ls ~/recon-base/ops/plan-charlie/ output: contingency.txt objectives.txt hint: plan-c matches only plan-charlie. Type ls ~/recon-base/ops/plan-c, press Tab, then Enter.

c broke the tie. plan-c matches only plan-charlie, so one Tab carried you the rest of the way, and you never typed the word charlie at all.

That was the whole lesson with the scaffolding gone: you picked the prefix, you predicted the match, and the shell did the typing.

You earned this. Two keystrokes, a lifetime of saved typing:

Tab works after any command, not just ls. The same key completes paths for cd, cat, and every command you will ever learn. It even completes command names themselves.

The habit that separates fast terminal users from slow ones: type two or three letters, then reach for Tab before you type another key. Let the shell do the rest.

You just drove recon-base with Tab in the practice terminal: doc plus Tab to reach documents, plan- and a double-Tab to see the choices, one letter and a single Tab to finish plan-alpha. Then you beat the comms and plan-charlie challenges with no command shown. Every one of those you typed yourself.

This module has one real Linux machine waiting for all of it: the Navigation mission lab. There the Tab key is live, and you will run these exact moves for real, on a live recon-base, alongside the other navigation skills. The mission shows no commands. You read each objective, type a few letters, press Tab, and watch the shell finish the name in front of you, exactly the habit you just practiced here.

A few letters, one keystroke, the shell types the rest. When you reach the Navigation mission, you will feel it for real.

Practice Tab Completion in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.