Skip to lesson

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.

Beginner to intermediate·12 min·Navigating the filesystem·Updated 2026-11-24

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:

text
history
text
  101  cd project
  102  ls -l
  103  grep ERROR logs/app.log

Each entry has a number. Type !103 to run entry 103 again. Other history shortcuts:

  • !! repeats the previous command. A common use is sudo !!, to repeat the last command with administrator rights.
  • !$ is the last argument of the previous command. After mkdir reports, the command cd !$ moves into it.
  • !grep runs the most recent command that started with grep.

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:

bash
echo file{1..3}.txt
echo {red,green,blue}.css
echo report-{2025,2026}-{a,b}
result
file1.txt file2.txt file3.txt
red.css green.css blue.css
report-2025-a report-2025-b report-2026-a report-2026-b

This 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:

bash
mkdir -p ~/project/archive
cd $_
pwd
result
/home/learner/project/archive

Common 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.

0 / 4 understood
01 What does pressing Tab do after you type part of a file name?
02 Which shortcut searches your command history as you type?
03 What does Ctrl+C do while a command is running?
04 What does echo file{1..3}.txt print?

Ready for the next small win?

Mark the lesson complete when you are ready.

Course navigation

Tools and Workflow

Linux Command Line tutorial

Learn to work confidently in the terminal, from your first command to pipes, permissions, scripts and a real log-analysis project.

Your progress 0/43
Learning something new?Explore all courses