Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy text to the clipboard from the OS/X terminal

Tags:

shell

macos

iterm

I am often having to copy and paste text from the terminal. Is there a way to redirect the output of a command into a shell variable or another command that would place the output in the clipboard?

like image 386
xvtk Avatar asked Aug 05 '12 09:08

xvtk


People also ask

How do I copy file content to clipboard Mac?

Use "pbcopy" to copy content of file to your clipboard. the command will copy all content from "file. txt" directly on your clipboard.

How do I copy and paste text in terminal?

Similarly, you can use Ctrl+shift+C to copy text from the terminal and then use it to paste in a text editor or web browser using the regular Ctrl+V shortcut. Basically, when you are interacting with the Linux terminal, you use the Ctrl+Shift+C/V for copy-pasting.

How do I enable select copy in Mac terminal?

In Terminal, select something and then within the same Terminal window do one of the following: click and drag the selection (a bit; the mouse pointer will change), and release to paste (hit Esc while dragging to cancel) or: paste using Shift-Command-V.

How do you use clipboard on macOS?

Anytime you use the common shortcuts Command/⌘+X (cut) or Command/⌘+ C (copy), your Mac handily stores what you copy onto the Clipboard. This saved content is later made available when you use Command/⌘+V (paste) shortcut.


2 Answers

Use pbcopy and pbpaste. Anything sent to pbcopy goes into the clipboard. Running pbpaste sends the contents of the clipboard to standard output and you can chain them just like all other commands.

You can find some example uses here: http://osxdaily.com/2007/03/05/manipulating-the-clipboard-from-the-command-line/

like image 76
Qsario Avatar answered Oct 18 '22 20:10

Qsario


pbcopy and pbpaste replace non-ASCII characters with question marks in some environments. It can be avoided by setting LC_CTYPE to UTF-8.

LC_CTYPE=UTF-8 pbpaste

You could also use osascript:

osascript -e 'on run {input}
set the clipboard to input
end' "ä"

osascript -e 'the clipboard as text'
like image 35
Lri Avatar answered Oct 18 '22 19:10

Lri