# Terminal Keyboard Shortcuts Most developers use only a fraction of available terminal keyboard shortcuts. Mastering these will dramatically speed up your command-line workflow. ## What Layer Do These Work On? These shortcuts operate at the **shell layer** (zsh/bash), not the terminal emulator or tmux. They work in any terminal, on any system, because they're part of the readline/emacs keybinding standard. See [[Dev Environment Stack]] for the full layer model. ## Essential Shortcuts You Should Use Daily ### Line Navigation | Shortcut | Action | Why It Matters | |----------|--------|----------------| | **Ctrl+A** | Jump to start of line | Faster than holding ← | | **Ctrl+E** | Jump to end of line | Faster than holding → | | **Option+←** | Move back one word | Navigate long commands quickly | | **Option+→** | Move forward one word | Navigate long commands quickly | **Example:** ```bash # You typed this long command: docker run -it --rm -v $(pwd):/workspace myimage # Cursor is at end. You need to change 'myimage' # Instead of: ← ← ← ← ← ← ← (14 presses) # Just do: Option+← (1 press!) ``` ### Deletion Shortcuts | Shortcut | Action | Memory Aid | |----------|--------|------------| | **Ctrl+W** | Delete word backward | "Word back" | | **Ctrl+U** | Delete to start of line | "Undo to start" | | **Ctrl+K** | Delete to end of line | "Kill to end" | | **Option+Backspace** | Delete word backward | Like Ctrl+W on Mac | | **Option+D** | Delete word forward | "Delete word" | **Example:** ```bash # You typed: git commit -m "fixed bug in authentication system" # You want to change the message # Instead of: Backspace × 35 # Just do: Ctrl+U (clears whole line) # Retype: git commit -m "add user authentication" ``` ### Editing Shortcuts | Shortcut | Action | Use Case | |----------|--------|----------| | **Ctrl+T** | Swap last two characters | Fix typos like "teh" → "the" | | **Option+T** | Swap last two words | Fix word order mistakes | | **Ctrl+Y** | Paste (yank) last deletion | Recover deleted text | | **Ctrl+_** | Undo last edit | Made a mistake? Undo it! | **Example:** ```bash # You typed: mv file.txt backup/ # Oops, order is wrong. Instead of retyping: Option+T # Swaps to: mv backup/ file.txt ``` ### History and Search | Shortcut | Action | Power Move | |----------|--------|------------| | **Ctrl+R** | Reverse history search | Find commands you ran weeks ago | | **Ctrl+G** | Cancel reverse search | Exit search without running | | **Ctrl+P** | Previous command | Same as ↑ | | **Ctrl+N** | Next command | Same as ↓ | | **Option+.** | Insert last argument | Reuse file paths | **Example:** ```bash # You ran this yesterday: docker exec -it mycontainer bash # Today you need it again # Instead of: ↑ ↑ ↑ ↑ ↑ (searching forever) # Just do: Ctrl+R, type "docker exec", Enter ``` **Power move with Option+.:** ```bash $ cat /very/long/path/to/config.yaml # Output shown... $ nvim Option+. # Expands to: nvim /very/long/path/to/config.yaml ``` ## Advanced Techniques ### Kill Ring (Cut and Paste) When you use Ctrl+W, Ctrl+U, or Ctrl+K, the deleted text goes into a "kill ring". You can paste it back with Ctrl+Y. ```bash # Type a long path /Users/ntiruviluamala/Code/github.com/theslyprofessor/midimaze # Delete it Ctrl+U # Type a command cd # Paste the path back Ctrl+Y # Result: cd /Users/ntiruviluamala/Code/github.com/theslyprofessor/midimaze ``` ### Repeat Last Argument ```bash $ mkdir new-project $ cd Option+. # Expands to: cd new-project ``` ### Transpose Characters for Typos ```bash # You typed: gti status # Cursor is after "gti" Ctrl+T # Swaps to: git status ``` ## Mac-Specific Notes ### Option Key Configuration On Mac, you might need to configure your terminal to use Option as Meta key: **In Ghostty:** Check terminal preferences **In iTerm2:** Preferences → Profiles → Keys → Left/Right Option key → Esc+ If Option+← inserts weird characters instead of moving by word, you need to enable this setting. ### Homebrew Shell vs System Shell macOS ships with old bash. Install modern zsh via Homebrew: ```bash brew install zsh chsh -s /usr/local/bin/zsh # or /opt/homebrew/bin/zsh on Apple Silicon ``` ## Complete Reference Table | Category | Shortcut | Action | |----------|----------|--------| | **Navigation** | Ctrl+A | Start of line | | | Ctrl+E | End of line | | | Option+← | Back one word | | | Option+→ | Forward one word | | **Deletion** | Ctrl+W | Delete word back | | | Option+Backspace | Delete word back (Mac) | | | Option+D | Delete word forward | | | Ctrl+U | Delete to line start | | | Ctrl+K | Delete to line end | | **Edit** | Ctrl+T | Swap last 2 chars | | | Option+T | Swap last 2 words | | | Ctrl+Y | Paste (yank) | | | Ctrl+_ | Undo | | **History** | Ctrl+R | Reverse search | | | Ctrl+G | Cancel search | | | Ctrl+P | Previous command | | | Ctrl+N | Next command | | | Option+. | Last argument | | **Process** | Ctrl+C | Kill current process | | | Ctrl+D | Exit shell | | | Ctrl+Z | Suspend process | | | Ctrl+L | Clear screen | ## Practice Exercise Try editing this command using only keyboard shortcuts: ```bash docker run -it --rm -v $(pwd):/workspace --name mycontainer ubuntu:latest bash ``` Tasks: 1. Change `ubuntu:latest` to `node:18` 2. Change `mycontainer` to `dev-env` 3. Delete the `--name` flag entirely 4. Add `-p 3000:3000` after `--rm` **Solutions:** 1. Option+← (twice to get to ubuntu), Ctrl+W, type `node:18` 2. Option+← (several times), Ctrl+W, type `dev-env` 3. Option+← to `--name`, Option+← Option+← to select both words, Ctrl+W Ctrl+W 4. Navigate to after `--rm`, type `-p 3000:3000` **The key insight:** With practice, you'll edit commands 3-5× faster than using arrow keys and backspace. **Related:** - [[Shell Line Editing with Zsh]] - Configure your shell - [[Dev Environment Stack]] - Understanding the four layers - [[tmux - Terminal Multiplexer]] - Multiplexer keybindings (different layer)