Tags: bash cheatsheet code
A cheat sheet for working faster at the command line, with macOS extras.
1. Editing the command line
| Shortcut | What it does |
|---|---|
Ctrl+A |
Jump to the start of the line |
Ctrl+E |
Jump to the end of the line |
Ctrl+W |
Delete the previous word |
Ctrl+U |
Delete from the cursor to the start of the line |
Ctrl+K |
Delete from the cursor to the end of the line |
Ctrl+Y |
Paste back whatever you just deleted with Ctrl+U,
Ctrl+K, or Ctrl+W |
Alt+B / Alt+F |
Move back or forward one word (needs Option as Meta; see setup notes) |
Ctrl+X, Ctrl+E |
Open the current command in $EDITOR; handy for long
commands |
Ctrl+L |
Clear the screen (keeps your current line) |
2. History
| Shortcut | What it does |
|---|---|
Ctrl+R |
Search your command history |
Alt+. |
Reuse the last argument of the previous command |
!! |
Repeat the last command |
sudo !! |
Rerun the last command as root |
!$ |
Last argument of the previous command (same idea as
Alt+.) |
!abc |
Rerun the most recent command that started with βabcβ |
^old^new |
Rerun the previous command, replacing the first βoldβ with βnewβ |
history \| grep word |
Find an old command by keyword |
3. Navigation
| Command | What it does |
|---|---|
cd - |
Jump back to the previous directory |
cd |
Go to your home directory |
pushd dir / popd |
Keep a stack of directories and hop back to them |
Tab Tab |
Show all possible completions |
4. Jobs and processes
| Shortcut | What it does |
|---|---|
Ctrl+C |
Stop the running command |
Ctrl+Z |
Suspend the running command |
bg |
Resume a suspended job in the background |
fg |
Bring a job back to the foreground |
jobs |
List background and suspended jobs |
Ctrl+D |
End input, or exit the shell (on an empty line) |
5. Expansion and chaining
| Example | What it does |
|---|---|
cp file.txt{,.bak} |
Brace expansion: copies to file.txt.bak |
mkdir -p project/{src,docs,tests} |
Create several folders at once |
cmd1 && cmd2 |
Run cmd2 only if cmd1 succeeds |
cmd1 \|\| cmd2 |
Run cmd2 only if cmd1 fails |
$(command) |
Use a commandβs output inside another command |
type name |
Show whether something is an alias, function, or program, and where it lives |
6. macOS extras
| Command | What it does |
|---|---|
open . |
Open the current folder in Finder |
open -a CotEditor file.txt |
Open a file in a specific app |
pbcopy / pbpaste |
Pipe to or from the clipboard,
e.g.Β cat file.txt \| pbcopy |
brew list |
See what Homebrew has installed |
python3 -m http.server |
Serve the current folder at localhost:8000 |
7. Setup notes and caveats
- Alt/Option as Meta.
Alt+.,Alt+B, andAlt+Fdonβt work in macOS Terminal by default.- Terminal.app: Settings β Profiles β Keyboard β enable βUse Option as Meta keyβ.
- iTerm2: Profiles β Keys β set Left Option key to βEsc+β.
- Workaround: press
Esc, then the key (e.g.ΒEscthen.).
- Old bash on macOS. macOS ships bash 3.2, so newer
features such as
**recursive globbing (shopt -s globstar) need a Homebrew bash:brew install bash. Ctrl+X,Ctrl+Euses whatever$EDITORor$VISUALis set to. If neither is set, bash falls back to a terminal editor like vi or nano.