CIS120 Linux Fundamentals by Scott Shaper

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

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

  1. Press Ctrl+R. The prompt may change to something like (reverse-i-search)'':.
  2. Type a few characters that appear in the command you want (e.g. cd, chapter4, or file1).
  3. The shell shows the most recent matching command. If that's the one you want, press Enter to run it.
  4. 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.
  5. 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:

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

Common Mistakes to Avoid

Best Practices