Read

Module 1 · First steps in the terminal

Tab completion, history and editing the command line

The keystrokes that make the shell fast: let Tab finish names for you, recall and fix earlier commands, and move around a line without the arrow keys.

What you will learn

  • Complete command and file names with Tab.
  • Recall, edit and re-run previous commands.
  • Use the basic line-editing shortcuts.

Nobody types full paths. Typing /root/lab/l8/a-very-long-directory-name/ by hand is slow and error-prone, and the shell already knows what is on disk. Press Tab after the first few letters and it finishes the name for you. This is not a convenience; it is how the command line is meant to be used, and it doubles as a spell checker: if Tab does nothing, the thing you are typing does not exist.

How Tab decides

The shell looks at the word under the cursor. If it is the first word of the line, it completes command names: mkd plus Tab becomes mkdir. Anywhere else it completes file and directory names relative to what you have typed so far. If exactly one name matches, it is filled in — with a trailing / for a directory, so you can keep going deeper. If several match, Tab fills in the common part and stops; press Tab a second time to see the list of candidates, type one more letter to disambiguate, and Tab again.

~% ls lab/l8/
a-very-long-directory-name-nobody-wants-to-type  report-2024-final-version-2.txt
~% cd lab/l8/a<Tab>
~% cd lab/l8/a-very-long-directory-name-nobody-wants-to-type/
~% ls lab/l8/r<Tab>
~% ls lab/l8/report-2024-final-version-2.txt

A rhythm develops: a few letters, Tab, a few letters, Tab. If Tab beeps or does nothing, stop and check your spelling or your position with ls — do not keep typing. The practice machine's shell completes names but not options; bash on a desktop can do options too, with extra packages.

History

Every line you run is remembered. The Up arrow brings back the previous one, Up again the one before, Down goes forward. The recalled line is editable: fix the typo, press Enter, and the corrected command runs. history prints the list with numbers. The list is saved to ~/.ash_history (.bash_history under bash) when the shell exits, so on a normal system it survives logouts; on this temporary machine it vanishes with everything else.

Moving inside the line

KeysEffect
TabComplete the current word; twice to list candidates.
Up / DownPrevious / next command from history.
Ctrl-A / Ctrl-EJump to the start / end of the line.
Ctrl-WDelete the word before the cursor.
Ctrl-UDelete everything before the cursor: a clean slate.
Ctrl-KDelete from the cursor to the end.
Ctrl-CAbandon the line (or interrupt a running command) and get a fresh prompt.
Ctrl-LClear the screen, keeping the current line.

These are Emacs-style bindings and they work in bash, zsh, BusyBox ash, Python's prompt, psql and most other line-oriented programs — learning them once pays everywhere. The most valuable single habit is Ctrl-C to bail out: whenever a line has gone wrong, or a command is taking too long, Ctrl-C returns you to a clean prompt.

Commands in this lesson

CommandWhat it does
<Tab>Complete the word under the cursor.
<Up> / <Down>Walk through previous commands.
historyPrint the numbered list of past commands.
Ctrl-A / Ctrl-EStart / end of line.
Ctrl-W / Ctrl-UDelete the previous word / the whole line before the cursor.
Ctrl-CAbandon the current line or interrupt the running command.

Quiz

  1. You type `cd /ro` and press Tab. What happens?

    • The shell completes it to `cd /root/`
    • The shell runs `cd /ro` and fails
    • Nothing; Tab only completes command names
  2. Tab fills in part of a name and stops. What should you do?

    • Press Tab again to see the candidates, then type a letter to disambiguate
    • Press Enter; the shell will pick one
    • Delete the line and start over
  3. Which key combination abandons the line you are typing and gives a fresh prompt?

    • Ctrl-A
    • Ctrl-C
    • Ctrl-E
  4. What does `history` print?

    • The files you edited recently
    • A numbered list of the commands you have run
    • The system's boot log
  5. Why does `!!` not work on the practice machine?

    • History is disabled
    • History expansion is a bash feature that ash lacks; use the Up arrow
    • Root is not allowed to repeat commands

Practice

  1. Print the list of commands you have run so far.

  2. Using Tab to complete the name, create an empty file called `done.txt` inside /root/lab/l8/a-very-long-directory-name-nobody-wants-to-type.

  3. /root/lab/l8 contains `report-2024-final-version-2.txt`. Create a sibling file with the same name but the extension `.bak` — let Tab type the long name, then edit the end of the line.

Open this lesson in the app to do the tasks in a real Linux machine and have them checked.