Linux Command Line · Lesson 8 of 43
Tab completion, history and keyboard shortcuts
Work much faster in the Bash shell with tab completion, command history, Ctrl+R search, line-editing shortcuts and brace expansion.
UNDERSTAND IT. THEN TRY IT.
What you can do after this lesson
Typing long paths and repeating commands by hand is slow and error-prone. In this lesson you learn the shortcuts that experienced users rely on all day: completion, history and line editing. They are small habits with a big payoff.
Tab completion
Type the first letters of a command or file name and press Tab. If only one name matches, the shell finishes it for you. If several match, press Tab twice to list them. For example, typing cd pro and pressing Tab completes to cd project/, and typing ls project/d followed by two Tabs lists data/ and docs/.
Completion also works for commands (gre then Tab gives grep), for variables, and for options on many systems when the bash-completion package is installed. It prevents typos, because the shell only completes names that really exist. If Tab does nothing or lists too much, type a few more letters and try again.
Tab completion is part of the interactive shell, so we cannot show it as command output. Practise it on your own machine.
Command history
Bash remembers the commands you ran. The simplest way to reuse them is the Up arrow, which brings back the previous command, and again for earlier ones; Down moves forward. You can also list them all:
history 101 cd project
102 ls -l
103 grep ERROR logs/app.logEach entry has a number. Type !103 to run entry 103 again. Other history shortcuts:
!!repeats the previous command. A common use issudo !!, to repeat the last command with administrator rights.!$is the last argument of the previous command. Aftermkdir reports, the commandcd !$moves into it.!grepruns the most recent command that started withgrep.
Bash saves history in the file ~/.bash_history when you leave the shell. Be careful: anything you type, including a password typed as part of a command, may be saved there.
Searching history with Ctrl+R
When history is long, searching beats scrolling. Press Ctrl+R and start typing any part of an old command. Bash shows the latest match instantly. Press Ctrl+R again to move to older matches, Enter to run the command, or the Right arrow to edit it first. Ctrl+G cancels the search.
Editing the line
You do not have to hold Backspace to fix a typo at the start of a long command. These shortcuts work in the line you are typing:
| Keys | Action |
|---|---|
| Ctrl+A / Ctrl+E | Jump to the start / end of the line |
| Alt+B / Alt+F | Move back / forward one word |
| Ctrl+W | Delete the word before the cursor |
| Ctrl+U / Ctrl+K | Delete to the start / end of the line |
| Ctrl+L | Clear the screen |
| Ctrl+C | Cancel the current command or line |
| Ctrl+D | Send end of input; on an empty line it exits the shell |
On macOS Terminal the Alt shortcuts may need the "Use Option as Meta key" setting.
Brace expansion
Before running a command, the shell expands curly braces into several words. It is a way of typing less when names follow a pattern:
echo file{1..3}.txt
echo {red,green,blue}.css
echo report-{2025,2026}-{a,b}file1.txt file2.txt file3.txt
red.css green.css blue.css
report-2025-a report-2025-b report-2026-a report-2026-bThis is very handy for creating several files or folders in one command, as the next section of the course shows. Note that the shell does this, not echo, so any command can use it.
$_ and the last argument
In scripts and in your terminal, $_ holds the last argument of the previous command, which lets you chain a creation and a move:
mkdir -p ~/project/archive
cd $_
pwd/home/learner/project/archiveCommon mistakes
Pressing Tab once and giving up. If nothing happens, press it twice to list the matches, or type another letter.
Using !! carelessly. Always check what !! expands to, especially after a dangerous command. Bash prints the expanded line before running it.
Putting secrets in commands. Passwords typed on the command line are stored in history and visible to other users in the process list.
Confusing Ctrl+C and copy. In the terminal, copy and paste are Ctrl+Shift+C and Ctrl+Shift+V (or Cmd+C/Cmd+V on macOS Terminal).
Exercise
Open your terminal and type cd /us then press Tab. Press Tab twice after ls /e. Run a few commands, then search for one of them with Ctrl+R. Use echo {a,b,c}{1,2} to see the combinations. Finally, create a folder with mkdir test-folder and enter it using cd !$ (or cd $_).
KNOWLEDGE CHECK
A quick check. You’ve got this.
Try each question. Every answer comes with an explanation.
Ready for the next small win?
Mark the lesson complete when you are ready.