Last Updated:

Spice up your CLI! Part 3: shortcuts & useful commands

⏮ Click here for Part 1: iTerm2 & ZSH 
⏮ Click here for Part 2: oh-my-zsh, themes & plugins

In Part 3 of this series, we'll be exploring some useful CLI shortcuts and commands. Most of the shortcuts should work across most shells and operating systems, but some may be specific to MacOS. The commands used are mostly unique to MacOS.

Useful CLI Shortcuts

1. Clear terminal

Instead of typing the clear command:

CTRL + L

2. Close terminal

Instead of typing logout or exit:

CTRL + D

3. Repeat the last field from the previous command

ESC + .

Alternatively, this will also be substituted in your command if you enter:

$_

4. Delete word-by-word:

CTRL + W

5. Delete entire line:

CTRL + Q

6. Go to beginning of line:

CTRL + A 

7. Go to end of line:

CTRL + E

8. Made a mistake while typing in a password on the CLI? This will delete the entered text without having to rerun the command:

CTRL + U

9. Reverse history search (from previous commands). Keep pressing to scroll back:

CTRL + R

10. Repeat the entire last command:

!!

Can also be used where you may have forgotten to use sudo, ie:

sudo !!

11. Kill a stale SSH connection

ENTER + ~ + .

Useful CLI Commands

1. Copy text from CLI, or read from file to clipboard:

echo "some text" | pbcopy

somecommand_that_prints_output | pbcopy

cat somefile.txt | pbcopy

2. Paste text from clipboard to CLI to stdout, or append to file:

pbpaste | less

pbpaste >> somefile.txt

3. Open the current directory in Finder:

open .

4. Open an executable from CLI:

open /Applications/Arduino.app/Contents/MacOS/Arduino

5. Keep your Mac awake/prevent it from locking:

caffeinate

Only for 30 minutes:

caffeinate -t 1800

6. Clear DNS cache

dscacheutil -flushcache

7. Speech Synthesis/Text-to-Speech (TTS)

say 'Hello from the command line'

or to speak from a file:

cat somefile.txt | say

8. Set volume (percent)

osascript -e "set volume output volume 50"

9. Create a desktop notification:

osascript -e 'display notification "message body text goes here" with title "this is my title"'

10. Display a prompt

osascript -e 'display alert "Prompt Title!" message "prompt text"'

11. Take a screencapture of everything:

screencapture -P

or by window, interactively, to file:

screencapture -o -W ~/screencapture.png