• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Copy and paste from the command line

Posted Feb 22, 2023 — Updated Jan 10, 2024

We use copy and paste all the time, so why not use them from the command line? I do it all the time, so let’s do a quick run down.

First, the commands:

  • Command line copy: pbcopy1
  • Command line paste: pbpaste

Copy something from the web and paste it to a file

This is my most frequent usage of either of these commands, specifically pbpaste. Imagine you’re at work looking at some code or some data in your browser, and you want to save it locally. Let’s use jsonplaceholder api data as an example:

example json data being copied
Example json data in chrome

The screenshot above is me copying data from my browser. I’d like to put that data directly into a file on my system:

# Paste the json in your terminal and redirect the output into a file
# This file doesn't need to exist already
pbpaste > example.json

That’s it. Now the contents of example.json are exactly the same json I just copied.

Copy the output of a command directly to your clipboard

Let’s say someone asks you: “Can you find me references to {thing} in our code?” An obscure request, but sure we can do that using grep (note: you should use ripgrep instead):

# Search for "thing", case-insensitive, and then pipe it into pbcopy
grep -i thing ./* | pbcopy

Now you can jump into slack or email or wherever and just paste the result. I find I do something like this with files more often:

Copy the contents of a file to your clipboard

# cat (print) the file and then pipe the output to your clipboard
cat example.json | pbcopy

# or alternatively, use input redirection "<"
# this way feels awkward to me since it breaks the usual
# left-to-right command line pattern
pbcopy < example.json

Now the contents of example.json are on your clipboard, ready to be pasted wherever your heart desires.


Helpful links

Pipes on the linux command line – Redhat

Notes

  1. It’s pbcopy and pbpaste because apparently it’s really called “pasteboard” instead of “clipboard”. Who knew? ↩︎

Filed Under: Command Line, Productivity, Tips

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 · Contact me at justin [at] {this domain}

  • Privacy Policy