Copy & Paste – The Tricky Way on macOS Terminal

Every of us knows copy & paste by ⌘-C and ⌘-V. Today I want to introduce you the pbcopy and pbpaste commands, for macOS exclusively.

pbcopy

Imagine that you want to copy the content of a file that has more than 1,000 lines on the termin…


This content originally appeared on DEV Community and was authored by Tim Wong

Every of us knows copy & paste by ⌘-C and ⌘-V. Today I want to introduce you the pbcopy and pbpaste commands, for macOS exclusively.

pbcopy

Imagine that you want to copy the content of a file that has more than 1,000 lines on the terminal.

One way is to cat /path/to/file, and use your mouse/trackpad to scroll and highlight the content, then press ⌘-C to copy. However, this seemingly simple task could be very annoying because scroll-and-highlight isn't so straight-forward sometimes.

A much simpler way is to pipe the output to pbcopy.

cat /path/to/file | pbcopy

Now you can ⌘-V the copied content anywhere. 🥳

pbpaste

You can echo the copied content by pbpaste. For example, you can save the copied content into a file.

pbpaste > /path/to/file

Or, process the copied content before saving it.

# Remove the empty lines
pbpaste | egrep -v '^$' > /path/to/file

Or, process the copied content AND copy the new content with pbcopy.

pbpaste | egrep -v '^$' | pbcopy

Summary

pbcopy and pbpaste is just a small trick, but very useful in my day-to-day work. Hope they could become your favourite commands too! 😎


This content originally appeared on DEV Community and was authored by Tim Wong


Print Share Comment Cite Upload Translate Updates
APA

Tim Wong | Sciencx (2021-10-24T14:15:18+00:00) Copy & Paste – The Tricky Way on macOS Terminal. Retrieved from https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/

MLA
" » Copy & Paste – The Tricky Way on macOS Terminal." Tim Wong | Sciencx - Sunday October 24, 2021, https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/
HARVARD
Tim Wong | Sciencx Sunday October 24, 2021 » Copy & Paste – The Tricky Way on macOS Terminal., viewed ,<https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/>
VANCOUVER
Tim Wong | Sciencx - » Copy & Paste – The Tricky Way on macOS Terminal. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/
CHICAGO
" » Copy & Paste – The Tricky Way on macOS Terminal." Tim Wong | Sciencx - Accessed . https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/
IEEE
" » Copy & Paste – The Tricky Way on macOS Terminal." Tim Wong | Sciencx [Online]. Available: https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/. [Accessed: ]
rf:citation
» Copy & Paste – The Tricky Way on macOS Terminal | Tim Wong | Sciencx | https://www.scien.cx/2021/10/24/copy-paste-the-tricky-way-on-macos-terminal/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.