Skip to content

Instantly share code, notes, and snippets.

@noamtamim
Last active April 12, 2021 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamtamim/926932d440969cd799da742ebc6b31be to your computer and use it in GitHub Desktop.
Save noamtamim/926932d440969cd799da742ebc6b31be to your computer and use it in GitHub Desktop.
macOS Terminal Tricks

Clipboard

pbpaste

This command writes (pastes) the content of the clipboard to stdout.

To use - copy (cmd-c) some text; then in the terminal type pbpaste and enter:

$ pbpaste
Hello, World!

pbcopy

This is the inverse of pbpaste - copy text from stdin to the clipboard.

Copy the text Hello to the clipboard:

$ echo "Hello" | pbcopy

Go to a text editor and do cmd-v, you'll get "Hello".

Copy the content of README.md to the clipboard:

$ pbcopy < README.md

Same, using cat:

$ cat README.md | pbcopy

Copy my ssh public key:

$ pbcopy < ~/.ssh/id_rsa.pub 

Base64

Encode

base64 encodes stdin to stdout.

Encode some text:

$ echo '{"os":"mac"}' | base64

Outputs eyJvcyI6Im1hYyJ9Cg== to the console.

Encode a file:

$ base64 < file.bin

Encode the clipboard:

pbpaste | base64

If the clipboard contained Hello, the above will output SGVsbG8=.

Encode the clipboard into the clipboard:

pbpaste | base64 | pbcopy

The clipboard will contain SGVsbG8=.

Decode

Use base64 -D (uppercase D) the same way as base64.

echo 'eyJvcyI6Im1hYyJ9Cg==' | base64 -D

Outputs {"os":"mac"} to the console.

JSON

Python's JSON tool

Python's JSON module contains a CLI tool that reads JSON from stdin, validates it and pretty-prints to stdout.

$ echo '{"os":"mac"}' | python -mjson.tool
{
    "os": "mac"
}

Pretty-print from the clipboard:

$ pbpaste | python -mjson.tool

Pretty-print the clipboard into the clipboard:

$ pbpaste | python -mjson.tool | pbcopy

Go crazy

Base64-decode a JSON document from the clipboard and pretty-print it into the clipboard:

$ pbpaste | base64 -D | python -mjson.tool | pbcopy

If the clipboard contained eyJvcyI6Im1hYyJ9Cg== (base64 of {"os":"mac"}) before this step, it now contains the following:

{
    "os": "mac"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment