Copying & Pasting In CLI With pbcopy & pbpaste

The pbcopy and pbpaste are very handy commands to copy from the clipboard buffer or paste into standard out. Having a good control of these commands can make your workflow even more efficient.

Using the pipe command (need a refresher on the pipe command?) you can copy output directly from a source right into your clipboard:

curl -s https://my-json-server.typicode.com/typicode/demo/comments | pbcopy

If you use something like jq (we wrote about it here) you can take some JSON and format it all in one command:

function copyJson() {
    pbpaste | jq '.' $@ | pbcopy
}

The above command take any JSON in your clipboard, put it into a pretty format and copy it back to your clipboard, so it will make this:

[{"id":1,"body":"some comment","postId":1},{"id":2,"body":"some comment","postId":1}]

Look like this:

[
  {
    "id": 1,
    "body": "some comment",
    "postId": 1
  },
  {
    "id": 2,
    "body": "some comment",
    "postId": 1
  }
]

Read more about it here

Instagram Post