Terminal Efficiency
Once you're comfortable typing commands, the next step is to work faster. The shell gives you shortcuts to complete names, repeat past commands, and move around the line without retyping everything. This lesson covers tab completion, command history (including !! and !number), reverse search (Ctrl+R), and essential control keys: Ctrl+A, Ctrl+E, Ctrl+C, Ctrl+D, and Ctrl+L. Learning these will make you much more efficient at the terminal.
The examples below use the directories and files created by the course setup.sh script: ~/playground/chapter1 through ~/playground/chapter13 (and ~/assignments). You can try Tab completion and history with those paths and, in ~/playground/chapter4, with files like file1.txt and file2.txt. Run cd ~/playground/chapter4 to follow along with the examples that use the current chapter's files.
Note: When we write Ctrl+X, press and hold the Control key, then press the letter key. On Mac keyboards you may see "Control" or "ctrl"; use the Control key, not the Command (⌘) key.
Quick Reference
| Shortcut or Command | What It Does |
|---|---|
Tab |
Complete commands, file names, and paths |
history |
Show your command history (with numbers) |
!! |
Run the last command again |
!number |
Run the command with that history number |
Ctrl+R |
Search backward through history |
Ctrl+A |
Move cursor to start of line |
Ctrl+E |
Move cursor to end of line |
Ctrl+C |
Cancel the current command or line |
Ctrl+D |
End input (e.g. exit shell or finish cat) |
Ctrl+L |
Clear the screen |
Tab Completion
Tab completion lets the shell finish what you're typing. If you type part of a command name, file name, or path and press Tab, the shell tries to complete it. That means less typing and fewer typos.
How It Works
- Type the first few characters of a command, file, or directory name.
- Press Tab once. If there is exactly one match, the shell completes it.
- If there are multiple matches, press Tab twice to see a list of options. Then type a bit more to make the match unique and press Tab again.
- If nothing matches, the shell may beep or do nothing; check your spelling or path.
Tab Completion Examples
# Type part of a command and press Tab:
pw[TAB] → pwd
cd ~/pla[TAB] → cd ~/playground/
# If several names match, press Tab twice to list them:
ls ~/playground/ch[TAB][TAB]
chapter1/ chapter2/ chapter3/ chapter4/ ...
# Then add one more character and Tab again:
ls ~/playground/chapter[TAB] → e.g. ls ~/playground/chapter4/ (type 4 to get chapter4)
# or you get chapter1/ chapter2/ chapter3/ chapter4/ ... and choose by typing more
Completion is case-sensitive: Doc[TAB] and doc[TAB] can give different results. Use Tab often—it saves time and helps you avoid mistakes.
Command History and the history Command
The shell keeps a list of commands you've run in the current session (and often across sessions). You can view that list and run previous commands again without retyping them.
Viewing History
To see your command history (with numbers), run:
history
You'll see lines like:
42 cd ~/playground/chapter4
43 ls
44 cat file1.txt
45 pwd
The number on the left is the history number. You use it with !number to run that command again. (The example uses ~/playground/chapter4 and file1.txt, which the setup creates.) The exact number of lines kept depends on your shell and settings (often hundreds or thousands).
!! and !number
Two of the most useful history shortcuts are !! and !number.
| Shortcut | What It Does | Example |
|---|---|---|
!! |
Run the last command again | You ran ls -l; type !! and Enter to run ls -l again |
!number |
Run the command with that history number | !44 runs whatever command was line 44 in history |
Using !! and !number
# After running a command, run it again with !!
$ ls -la ~/playground/chapter4
(... output ...)
$ !!
ls -la ~/playground/chapter4
(... same command runs again ...)
# Run a specific command from history (use the number from 'history')
$ history
...
50 cat file1.txt
51 pwd
$ !50
cat file1.txt
(... command 50 runs ...)
!! is handy when you run a command and immediately want to run it again (for example after fixing a permission or path). !number is useful when you've run history and see the number of the command you want.
Ctrl+R (Reverse Search)
Ctrl+R opens a search mode so you can find a past command by typing part of it. The shell searches backward through your history and shows the most recent match. This is faster than scrolling through history when you remember a word or phrase from the command.
How to Use Ctrl+R
- Press Ctrl+R. The prompt may change to something like
(reverse-i-search)'':. - Type a few characters that appear in the command you want (e.g.
cd,chapter4, orfile1). - The shell shows the most recent matching command. If that's the one you want, press Enter to run it.
- If it's not the right one, press Ctrl+R again to go to the next older match. Keep pressing Ctrl+R to step further back.
- To cancel the search without running anything, press Ctrl+C.
Reverse Search Example
# You ran 'cat file1.txt' in ~/playground/chapter4 earlier and want to run it again.
# Press Ctrl+R, then type: cat
(reverse-i-search)'cat': cat file1.txt
# If this is the command you want, press Enter to run it.
# If an older command appears, press Ctrl+R again to see the next match.
Reverse search is especially useful for long or complex commands you don't want to retype.
Ctrl+A and Ctrl+E
These two keys move the cursor to the start or end of the current line. They work while you're typing or editing a command (before you press Enter).
| Key | What It Does | When to Use It |
|---|---|---|
Ctrl+A |
Move cursor to the beginning of the line | When you need to add something at the start (e.g. sudo or cd) |
Ctrl+E |
Move cursor to the end of the line | When you need to add something at the end (e.g. an option or filename) |
Using Ctrl+A and Ctrl+E
# You typed: cat file1.txt
# You realize you meant to run it with sudo. Press Ctrl+A, then type "sudo " at the start.
# Result: sudo cat file1.txt
# You typed: ls -l
# You want to add a path. Press Ctrl+E to jump to the end, then type the path.
# Result: ls -l ~/playground/chapter4
Using Ctrl+A and Ctrl+E is faster than holding the left or right arrow key, especially on long lines.
Ctrl+C, Ctrl+D, and Ctrl+L
These three control keys do different things: cancel a command, signal “end of input,” and clear the screen.
| Key | What It Does | When to Use It |
|---|---|---|
Ctrl+C |
Cancel the current command or line | Stop a running command; discard the line you're typing and get a new prompt |
Ctrl+D |
Send end-of-input | Exit the current shell; or when typing into cat > file, finish input (like “I’m done typing”) |
Ctrl+L |
Clear the screen | When the terminal is cluttered; prompt moves to top, output is cleared |
Ctrl+C (Cancel)
Press Ctrl+C to stop a command that's running (e.g. a long process or a command that's waiting for input). It also cancels the line you're typing: the shell discards what you typed and gives you a fresh prompt. Use it when you change your mind or make a mistake and want to start over.
Ctrl+D (End of Input)
Ctrl+D tells the shell “no more input.” Its effect depends on context:
- At an empty prompt: Often exits the shell (logs you out of that terminal session). So use Ctrl+D when you're done with that terminal window.
- While typing into
cat > filenameorcat >> filename: Ctrl+D ends your input and saves the file. You don't type any text after Ctrl+D on that line.
Don't press Ctrl+D at the prompt unless you intend to exit the shell.
Ctrl+L (Clear Screen)
Press Ctrl+L to clear the terminal screen. The prompt moves to the top and previous output is cleared (though you can still scroll up in most terminals to see it). Use it when the screen is full or distracting. It does not stop or cancel any command.
Tips for Success
- Use Tab often: For commands, paths, and filenames—it saves time and reduces typos.
- Run
historywhen you forget a command: Find the number and use!numberto run it again. - Use Ctrl+R for recent commands: Type a few letters you remember instead of scrolling through history.
- Use Ctrl+A / Ctrl+E to fix or extend a command instead of retyping the whole line.
- Use Ctrl+C to stop a command or cancel a line; use Ctrl+L when the screen is cluttered.
Common Mistakes to Avoid
- Forgetting that Tab is case-sensitive (e.g.
Documentsvsdocuments). - Using Ctrl+D at the prompt when you only meant to clear the screen (use Ctrl+L to clear).
- Retyping long commands instead of using
!!,!number, or Ctrl+R. - Not pressing Tab twice when completion doesn't happen—there may be multiple matches to choose from.
Best Practices
- Make Tab completion a habit for every command and path.
- Use
historyand!numberto repeat or tweak recent commands. - Use Ctrl+R to quickly find and rerun a command by a word or phrase.
- Use Ctrl+A and Ctrl+E to edit the current line instead of retyping it.
- Use Ctrl+C to cancel; use Ctrl+L to clear the screen; use Ctrl+D only when you mean “end input” or exit.