Here's just a quick list of useful keyboard shortcuts for the terminal/OS. Left column is the command, right column is the keyboard shortcut description. I've also included some of the commands I use the most.
| Key/Command | Description |
|---|---|
| ctrl + A | Jump to the start of the current command line. Works in most text fields system-wide (Netbeans is a notable exception). |
| ctrl + E | Jump to the end of the current command line. Works in most text fields system-wide (Netbeans is a notable exception). |
| ctrl + L | Wipe the terminal display |
| cmd + K | Wipe the terminal display |
| ctrl + U | Slice text from the cursor back to the beginning of the line |
| ctrl + K | Slice text from the cursor forward to the end of the line |
| ctrl + W | Slice one word behind the cursor, using whitespace as the boundary |
| ctrl + Y | Insert the most recently sliced text |
| ctrl + H | Erase the character left of the cursor (same as pressing Backspace) |
| ctrl + C | Halt the active process; also clears the entire line |
| ctrl + D | Send EOF or, if the shell is idle, terminate it |
| ctrl + Z | Suspend the foreground job; resume with fg |
| ctrl + _ | Reverse the previous edit (underscore = Shift + minus) |
| ctrl + T | Flip the two characters immediately before the cursor |
| ctrl + F | Shift cursor one character to the right |
| ctrl + B | Shift cursor one character to the left |
| option + → | Shift cursor one word to the right |
| option + ← | Shift cursor one word to the left |
| Esc + T | Flip the two words just before the cursor |
| Esc + Backspace | Slice one word behind the cursor, splitting on non-alphabetic chars |
| Tab | Finish file or folder names for you |
Basic Core Commands
| Key/Command | Description |
|---|---|
| cd [folder] | Switch to the named directory (e.g. cd Documents) |
| cd | Jump to your home folder |
| cd ~ | Jump to your home folder |
| cd / | Jump to the filesystem root |
| cd - | Jump to the previous working directory |
| ls | Compact listing |
| ls -l | Verbose listing |
| ls -a | Verbose listing including hidden items |
| ls -lh | Verbose listing with human-friendly size units |
| ls -R | Recurse through sub-directories and list everything |
| sudo [command] | Execute [command] with root privileges |
| open [file] | Launch a file as though you double-clicked it in Finder |
| top | Show live process stats (press q to exit) |
| nano [file] | Edit file in the nano text editor |
| vim [file] | Edit file in the vim text editor |
| clear | Empty the screen |
| reset | Re-initialize the terminal display |
CHAINING COMMANDS
| Key/Command | Description |
|---|---|
| [command-a]; [command-b] | Run A, then B—success or failure of A is ignored |
| [command-a] && [command-b] | Run B only if A exits successfully |
| [command-a] || [command-b] | Run B only if A fails |
| [command-a] & | Launch A in the background |
PIPING COMMANDS
| Key/Command | Description |
|---|---|
| [command-a] | [command-b] | Feed A’s stdout into B’s stdin (e.g. ps auxwww | grep google) |
COMMAND HISTORY
| Key/Command | Description |
|---|---|
| history n | Display recent commands—limit to last n if number supplied |
| ctrl + r | Search interactively through your shell history |
| ![value] | Re-run the most recent command starting with “value” |
| ![value]:p | Print (but don’t run) the most recent command starting with “value” |
| !! | Re-run the last command |
| !!.p | Print (but don’t run) the last command |
FILE MANAGEMENT
| Key/Command | Description |
|---|---|
| touch [file] | Create an empty file or update its timestamp |
| pwd | Show absolute path of current directory |
| . | Reference the current directory (ls .) |
| .. | Reference the parent directory (ls ..) |
| ls -l .. | Detailed listing of parent directory |
| cd ../../ | Move up two directory levels |
| cat | Dump file contents to stdout |
| rm [file] | Delete a file (e.g. rm data.tmp) |
| rm -i [file] | Delete with confirmation prompt |
| rm -r [dir] | Delete directory and everything inside |
| rm -f [file] | Force deletion without prompting |
| cp [file] [newfile] | Duplicate file to new name |
| cp [file] [dir] | Duplicate file into target directory |
| mv [file] [new filename] | Move or rename (e.g. mv file1.ad /tmp) |
| pbcopy < [file] | Send file contents to clipboard |
| pbpaste | Retrieve clipboard contents |
| pbpaste > [file] | Save clipboard into file (pbpaste > paste-test.txt) |
DIRECTORY MANAGEMENT
| Key/Command | Description |
|---|---|
| mkdir [dir] | Make a new directory |
| mkdir -p [dir]/[dir] | Create nested path in one shot |
| rmdir [dir] | Remove empty directory |
| rm -R [dir] | Remove directory and its contents |
| less [file] | Page through file content |
| [command] > [file] | Redirect stdout to file (overwrites) |
| [command] >> [file] | Append stdout to file |
| [command] < [file] | Read stdin from file |
SEARCH
| Key/Command | Description |
|---|---|
| find [dir] -name [pattern] | Hunt for files matching pattern (e.g. find /Users -name "file.txt") |
| grep [pattern] [file] | Display lines containing pattern (e.g. grep "Tom" file.txt) |
| grep -r [pattern] [dir] | Recursively grep through directory |
| grep -v [pattern] [file] | Show lines that do NOT match pattern |
| grep -i [pattern] [file] | Case-insensitive pattern match |
| mdfind [pattern] | Spotlight search across names, content, metadata (e.g. mdfind skateboard) |
| mdfind -onlyin [dir] -name [pattern] | Limit Spotlight search to given directory and filename pattern |
HELP
| Key/Command | Description |
|---|---|
| [command] -h | Display concise help |
| [command] --help | Display concise help |
| apropos [keyword] | Find commands related to keyword |
| info [command] | Browse info documentation |
| man [command] | Open the manual page for [command] |
| whatis [command] | One-line summary of [command] |
Sourced from terminal-mac-cheatsheet with some adjustments.