Command-line hotkeys for macOS:
Action | Hotkey |
---|---|
Autocomplete command | Tab |
Move cursor left/right by character | Control + B / Control + F |
Move cursor left/right by word | Option + ← / Option + → |
Move to beginning of line | Control + A |
Move to end of line | Control + E |
Delete previous character | Control + H |
Delete current character | Control + D |
Delete to end of line | Control + K |
Delete to beginning of line | Control + U |
Delete word to the left | Control + W |
Transpose (swap) two characters | Control + T |
Previous command in history | Control + P / ↑ |
Next command in history | Control + N / ↓ |
Search command history | Control + R |
Re-run last command | !! |
Re-run last command with word replacement | ^oldword^newword |
Clear the screen | Control + L |
Cancel current command | Control + C |
Suspend current command | Control + Z |
Copy | Command + C (text) |
Paste | Command + V |
This documentation provides a detailed guide to using hotkeys and navigating the command-line interface in macOS, with a focus on using the Terminal and VS Code integrated shell. By leveraging these shortcuts, you can efficiently manage commands, edit text, and access previous history.
Press the Tab
key while typing a command to autocomplete file names, directories, and commands. If multiple options exist, pressing Tab
twice will display a list of possible completions.
cd Doc[Tab]
This will autocomplete Documents/
if the folder exists.
Once you've typed a command, you can move around to edit it without deleting the whole line. The following key combinations are available:
-
Move cursor left/right by one character:
Control
+B
(backward) /Control
+F
(forward) -
Move cursor left/right by one word:
Option
+←
(left) /Option
+→
(right) -
Move cursor to the beginning of the line:
Control
+A
-
Move cursor to the end of the line:
Control
+E
-
Delete the character to the left of the cursor:
Control
+H
-
Delete the character under the cursor:
Control
+D
-
Delete from cursor to the end of the line:
Control
+K
-
Delete from cursor to the beginning of the line:
Control
+U
-
Delete the word to the left of the cursor:
Control
+W
-
Transpose (swap) two characters:
Control
+T
If you've typed sudo apt get install package
, you can move the cursor to quickly insert the missing -
in apt-get
without retyping the whole command.
macOS maintains a history of your previously entered commands, which you can navigate and reuse:
-
Previous command:
Control
+P
or↑
(up arrow) -
Next command:
Control
+N
or↓
(down arrow)
- Incremental search through history:
Control
+R
Start typing part of the command you want to recall, and the terminal will search through your history.
(reverse-i-search)`install': sudo apt-get install package
-
Re-run the last command:
!!
-
Re-run the last command but replace a word:
^oldword^newword
sudo apt-get install package
You can quickly rerun this command with a modification:
^install^remove
This will execute sudo apt-get remove package
.
- Clear the terminal display:
Control
+L
This does not delete any command history; it just clears the view for a cleaner workspace.
To run multiple commands sequentially, you can use:
-
Semicolon (
;
):
Execute one command after the other regardless of success:command1; command2
-
Double ampersand (
&&
):
Only execute the next command if the previous one succeeds:command1 && command2
-
Double pipe (
||
):
Execute the next command only if the previous one fails:command1 || command2
-
Copy selected text:
Command
+C
-
Paste:
Command
+V
Note: If the terminal is running a command, use Control
+ C
to cancel the process, not Command
+ C
, as this sends an interrupt signal to the process.
-
Cancel a command:
Control
+C
-
Suspend a process (pause):
Control
+Z
This stops the process and puts it in the background. You can resume it later using thefg
command.
If you are using the zsh
shell (which is the default shell starting from macOS Catalina), it offers command auto-suggestions based on your history. As you type, it shows possible commands in a faded color. Press →
to accept the suggestion.
By using these hotkeys and navigation tips, you can work more efficiently in the macOS command-line environment. Whether you are editing long commands, navigating history, or managing multiple commands, these features will improve your command-line productivity.
Generated by ChatGPT