⌨️Terminal Shortcuts & Productivity
Tab completion, history, shortcuts, and tricks that save hours
Terminal Shortcuts & Productivity
The difference between a beginner and a power user often comes down to shortcuts. Learning a handful of keyboard tricks can cut your terminal work time in half — or more. This lesson covers the essential shortcuts, history tricks, and productivity patterns every developer should know.
1. Tab Completion — The Most Important Shortcut
Tab completion is the single most important shortcut in the terminal. Instead of typing a full file name, path, or command, you press Tab and the shell finishes it for you.
How It Works
# Type the beginning of a file name, then press Tab
cd Doc[Tab]
# Shell completes it to:
cd Documents/
# If there are multiple matches, press Tab twice to see all options
cd D[Tab][Tab]
# Shows: Desktop/ Documents/ Downloads/Tab Completion Works For
- File and directory names — the most common use
- Command names — type
gi[Tab]to getgit - Git branches —
git checkout fea[Tab]completes togit checkout feature-branch - Environment variables —
echo $HO[Tab]completes toecho $HOME - SSH hostnames — if configured in your SSH config
- Package names — many package managers support tab completion
Why It Matters
- Speed — you type fewer characters
- Accuracy — the shell never misspells a filename
- Discovery — pressing Tab twice shows you what is available
- Confidence — if Tab completes, the path exists
Pro Tip
If Tab does nothing, the match is ambiguous. Press Tab again to see all possibilities. Then type one or two more characters and Tab again.
# Nothing happens on first Tab because multiple matches exist
ls pro[Tab]
# Press Tab again to see options:
# profile.txt project/ prometheus.yml
# Add one more letter and Tab again
ls proj[Tab]
# Completes to: ls project/2. Command History
Your shell remembers every command you run. Learning to navigate that history is a huge time saver.
Up / Down Arrow Keys
The simplest way to access history:
# Press Up Arrow to go to the previous command
# Press Up Arrow again to go further back
# Press Down Arrow to go forward in history
# Press Enter to re-run the displayed commandThe history Command
View your full command history:
# Show all history
history
# Show last 20 commands
history 20
# Search history for a specific command
history | grep "docker"
# Run a specific command by number
!42
# This runs whatever command was number 42 in your historyCtrl+R — Reverse Search (Game Changer)
This is the most powerful history feature. Press Ctrl+R and start typing any part of a previous command:
# Press Ctrl+R, then type "docker"
(reverse-i-search)\`docker\`: docker compose up -d
# Press Ctrl+R again to cycle through older matches
# Press Enter to run the found command
# Press Ctrl+C or Escape to cancel the search
# Press Right Arrow to edit the found command before runningWhy Ctrl+R Is So Powerful
Imagine you ran a complex command 200 commands ago:
kubectl exec -it my-pod-abc123 -- /bin/bash -c "cat /var/log/app.log | tail -50"Instead of typing all of that again, press Ctrl+R, type kubectl exec, and it appears instantly. Press Enter to run it.
3. Essential Keyboard Shortcuts
These shortcuts work in most Unix shells (Bash, Zsh) and are based on Emacs keybindings.
Canceling and Exiting
| Shortcut | Action | When to Use |
|---|---|---|
Ctrl+C | Cancel / kill current process | A command is stuck or you want to stop it |
Ctrl+D | Exit the shell / send EOF | Close terminal session or end input |
Ctrl+Z | Suspend current process | Pause a process (resume with fg) |
Clearing and Display
| Shortcut | Action | When to Use |
|---|---|---|
Ctrl+L | Clear the screen | Clean up a cluttered terminal |
clear | Same as Ctrl+L (command) | Alternative to the shortcut |
Cursor Movement
| Shortcut | Action | When to Use |
|---|---|---|
Ctrl+A | Move cursor to beginning of line | Jump to the start quickly |
Ctrl+E | Move cursor to end of line | Jump to the end quickly |
Alt+F | Move cursor one word forward | Navigate within a long command |
Alt+B | Move cursor one word backward | Navigate within a long command |
Deleting Text
| Shortcut | Action | When to Use |
|---|---|---|
Ctrl+U | Delete from cursor to beginning of line | Clear everything before cursor |
Ctrl+K | Delete from cursor to end of line | Clear everything after cursor |
Ctrl+W | Delete one word before cursor | Remove the last word you typed |
Alt+D | Delete one word after cursor | Remove the next word |
Ctrl+Y | Paste (yank) the last deleted text | Undo a Ctrl+U or Ctrl+K |
Example Workflow
Suppose you typed a long command but need to change the beginning:
# You typed:
npm run build --env=production --verbose --output=dist
# You realize you need sudo. Instead of using the arrow keys:
# 1. Press Ctrl+A to jump to the beginning
# 2. Type "sudo " before the command
# 3. Press Ctrl+E to jump back to the end if needed
# 4. Press Enter to run
sudo npm run build --env=production --verbose --output=dist4. History Shortcuts
!! — Repeat Last Command
# Run the last command again
!!
# Most common use: forgot sudo
apt install nginx
# Permission denied!
sudo !!
# Expands to: sudo apt install nginx!$ — Last Argument of Previous Command
mkdir /var/www/myproject
cd !$
# Expands to: cd /var/www/myproject!command — Run Last Command Starting With...
!git
# Runs the last command that started with "git"
!ssh
# Runs the last command that started with "ssh"Use With Caution
The ! shortcuts run commands immediately without confirmation. If you are not sure what the last matching command was, use Ctrl+R instead — it shows you the command before running it.
5. Wildcards — Match Multiple Files
Wildcards let you work with groups of files using patterns instead of listing each file individually.
The * Wildcard — Match Anything
# List all .txt files
ls *.txt
# Delete all .log files
rm *.log
# Copy all images to a folder
cp *.png *.jpg images/
# List all files starting with "report"
ls report*
# Match anything with "test" in the name
ls *test*The ? Wildcard — Match One Character
# Match file1.txt, file2.txt, file3.txt (but not file10.txt)
ls file?.txt
# Match any single character
ls report_?.pdf
# Matches: report_A.pdf, report_B.pdf
# Does not match: report_AB.pdfCombining Wildcards
# All JavaScript files in any subdirectory (Bash with globstar)
ls **/*.js
# All files with a three-letter extension
ls *.???6. Command Chaining
Run multiple commands in sequence using operators.
&& — AND (Run Next Only If Previous Succeeds)
# Build and then deploy — only deploy if build succeeds
npm run build && npm run deploy
# Create directory and enter it
mkdir myproject && cd myproject
# Update, upgrade, then clean
sudo apt update && sudo apt upgrade -y && sudo apt autoremove|| — OR (Run Next Only If Previous Fails)
# Try to find the file, or print an error message
cat config.json || echo "Config file not found!"
# Try the primary server, fall back to secondary
ping -c 1 primary.server || ping -c 1 backup.server; — Run Sequentially (Regardless of Success or Failure)
# Run both commands no matter what
echo "Starting..."; npm run build; echo "Done."
# Clean up and rebuild regardless
rm -rf dist; npm run buildChoosing the Right Operator
| Operator | Meaning | Use When |
|---|---|---|
&& | AND — next runs only on success | Steps depend on each other |
|| | OR — next runs only on failure | You need a fallback |
; | Sequential — always runs next | Steps are independent |
7. Piping — Connect Commands Together
The pipe operator | sends the output of one command as input to the next.
# List files and filter for .js files
ls -la | grep ".js"
# Count how many files are in a directory
ls | wc -l
# Find the 10 largest files
du -sh * | sort -rh | head -10
# Search processes for "node"
ps aux | grep "node"
# Get unique sorted entries from a file
cat data.txt | sort | uniq
# Chain multiple pipes
cat server.log | grep "ERROR" | sort | uniq -c | sort -rn | head -5
# This finds the 5 most frequent error messages in the logHow Piping Differs from Chaining
- Chaining (
&&) runs commands in sequence — command B starts after command A finishes - Piping (
|) connects commands — command B processes the output of command A as it streams
8. Redirecting Output
Send command output to files instead of the screen.
> — Write to File (Overwrite)
# Save directory listing to a file
ls -la > filelist.txt
# Save command output
echo "Hello World" > greeting.txt
# Save errors to a file
npm run build 2> errors.log>> — Append to File
# Add a line to an existing file
echo "New entry" >> log.txt
# Append date to a log
date >> activity.log
# Accumulate results
echo "Test 1: PASS" >> results.txt
echo "Test 2: FAIL" >> results.txtRedirect Both Output and Errors
# Send both stdout and stderr to a file
npm run build > build.log 2>&1
# Send output to one file, errors to another
npm run build > output.log 2> errors.log
# Discard all output (send to /dev/null)
command > /dev/null 2>&19. Aliases — Create Your Own Shortcuts
Aliases let you create short names for long or frequently used commands.
Creating Aliases
# Temporary alias (lasts until terminal closes)
alias ll="ls -la"
alias gs="git status"
alias gc="git commit -m"
# Use them like regular commands
ll
gs
gc "fix: update login flow"Permanent Aliases
Add aliases to your shell config file to make them permanent:
# For Zsh (macOS default)
nano ~/.zshrc
# For Bash
nano ~/.bashrc
# Add lines like:
alias ll="ls -la"
alias gs="git status"
alias gp="git push"
alias gl="git log --oneline --graph"
alias dc="docker compose"
alias k="kubectl"
alias python="python3"
alias pip="pip3"
# After editing, reload the config:
source ~/.zshrc # or source ~/.bashrcUseful Alias Ideas
# Navigation
alias ..="cd .."
alias ...="cd ../.."
alias home="cd ~"
alias projects="cd ~/Projects"
# Safety nets
alias rm="rm -i" # Confirm before deleting
alias cp="cp -i" # Confirm before overwriting
alias mv="mv -i" # Confirm before overwriting
# Git workflow
alias gst="git status"
alias gco="git checkout"
alias gbr="git branch"
alias gpl="git pull"
alias gps="git push"
alias glog="git log --oneline --graph --all"
# Development
alias serve="python3 -m http.server 8000"
alias ports="lsof -i -P -n | grep LISTEN"
alias myip="curl -s ifconfig.me"Viewing and Removing Aliases
# List all current aliases
alias
# Remove an alias
unalias ll10. Full Keyboard Shortcuts Reference
Here is a comprehensive reference table of the most useful terminal shortcuts:
Navigation
| Shortcut | Action |
|---|---|
Ctrl+A | Beginning of line |
Ctrl+E | End of line |
Alt+F | Forward one word |
Alt+B | Back one word |
Editing
| Shortcut | Action |
|---|---|
Ctrl+U | Delete to beginning of line |
Ctrl+K | Delete to end of line |
Ctrl+W | Delete previous word |
Alt+D | Delete next word |
Ctrl+Y | Paste last deleted text |
Ctrl+T | Swap current and previous character |
Alt+T | Swap current and previous word |
Alt+U | Uppercase current word |
Alt+L | Lowercase current word |
History
| Shortcut | Action |
|---|---|
Up Arrow | Previous command |
Down Arrow | Next command |
Ctrl+R | Reverse search history |
Ctrl+G | Cancel history search |
Ctrl+P | Previous command (same as Up) |
Ctrl+N | Next command (same as Down) |
!! | Repeat last command |
!$ | Last argument of previous command |
!* | All arguments of previous command |
!command | Last command starting with "command" |
Control
| Shortcut | Action |
|---|---|
Ctrl+C | Cancel current command |
Ctrl+D | Exit shell or send EOF |
Ctrl+Z | Suspend current process |
Ctrl+L | Clear screen |
Ctrl+S | Freeze terminal output |
Ctrl+Q | Resume terminal output |
11. Speed Comparison: Mouse vs. Keyboard
Here is a real-world comparison showing why keyboard shortcuts matter.
Task: Edit a File, Search for an Error, Fix It, Commit
Mouse-Heavy Workflow (Estimated: 45 seconds)
- Click on terminal window (1s)
- Type
cdand manually type the full path (5s) - Type
cat filename.txtand scroll to find the error (10s) - Open the file in a GUI editor, click to the right line (5s)
- Make the edit, save with mouse click (5s)
- Switch back to terminal, type full git commands (15s)
- Click through confirmation dialogs (4s)
Keyboard Workflow (Estimated: 15 seconds)
Ctrl+Rthen typecd proj— instantly recallscd ~/Projects/myapp(2s)grep -n "error" src/*.js— finds the line instantly (3s)vi +42 src/app.js— opens file at the exact line (1s)- Fix the error,
:wqto save and quit (4s) gst(alias) thengc "fix: resolve null check"(5s)
Result: 3x faster with keyboard shortcuts.
The Compound Effect
| Scenario | Mouse Workflow | Keyboard Workflow | Time Saved |
|---|---|---|---|
| Edit and commit | 45 seconds | 15 seconds | 30 seconds |
| 10 edits per day | 7.5 minutes | 2.5 minutes | 5 minutes |
| Over one month (22 days) | 2.75 hours | 0.92 hours | 1.83 hours |
| Over one year | 33 hours | 11 hours | 22 hours |
These numbers are conservative. Power users often report saving 100+ hours per year from terminal shortcuts alone.
Summary
The shortcuts covered in this lesson form the foundation of terminal productivity:
| Category | Key Takeaway |
|---|---|
| Tab completion | Always use Tab — it is faster and more accurate than typing |
| History | Ctrl+R is the most powerful history tool — learn it by heart |
| Navigation | Ctrl+A, Ctrl+E, Ctrl+W save constant arrow-key pressing |
| Canceling | Ctrl+C to cancel, Ctrl+D to exit — know the difference |
| Chaining | Use && for dependent steps, ; for independent steps |
| Piping | Connect commands with ` |
| Redirecting | Use > to write, >> to append output to files |
| Aliases | Create shortcuts for commands you use dozens of times a day |
The best way to learn these shortcuts is practice. Pick two or three new shortcuts today and force yourself to use them. Within a week they will be second nature.