Skip to content

Instantly share code, notes, and snippets.

@brennanMKE
Created October 10, 2024 17:15
Show Gist options
  • Save brennanMKE/13128ca369c45f4ed3fdc2b524656990 to your computer and use it in GitHub Desktop.
Save brennanMKE/13128ca369c45f4ed3fdc2b524656990 to your computer and use it in GitHub Desktop.
macOS Command Line Cheat Sheet

Command Line Tips for macOS

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

Command-Line Basics on macOS: Efficient Navigation and Hotkeys

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.

Command Completion and Editing

Tab for Command Autocompletion

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.

Example:

cd Doc[Tab]

This will autocomplete Documents/ if the folder exists.

Navigating and Editing Commands

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

Example:

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.

Command History Navigation

macOS maintains a history of your previously entered commands, which you can navigate and reuse:

Navigating Command History

  • Previous command:
    Control + P or (up arrow)

  • Next command:
    Control + N or (down arrow)

Search Command History

  • Incremental search through history:
    Control + R

Start typing part of the command you want to recall, and the terminal will search through your history.

Example:

(reverse-i-search)`install': sudo apt-get install package

Re-execute Commands

  • Re-run the last command:
    !!

  • Re-run the last command but replace a word:
    ^oldword^newword

Example:

sudo apt-get install package

You can quickly rerun this command with a modification:

^install^remove

This will execute sudo apt-get remove package.

Working with Multiple Commands

Clear the Screen

  • Clear the terminal display:
    Control + L

This does not delete any command history; it just clears the view for a cleaner workspace.

Running Multiple Commands on One Line

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

Additional Hotkeys and Tips

Copying and Pasting in the Terminal

  • 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 or Suspend Commands

  • 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 the fg command.

Auto-suggestion with zsh

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment