Terminal: To copy a line of text to general macOS clipboard

In Terminal, to copy a line of text so that it can be pasted back later, one can use the kill-ring way:

  1. Type the text you want to paste later
  2. Press Control-U to delete the line
  3. Press Control-Y to yank (= paste) this text back

But there is a pitfall: using this method, you won’t be able to paste this text to other apps, e.g., TextEdit or Keynote. That is, if you open TextEdit then, neither Command-V, nor Control-Y will paste this text there.

So, is it possible to copy a line of text in Terminal, using the keyboard only (that is, without selecting this text using the mouse or trackpad) so that it can be pasted not only in Terminal but also in TextEdit, Keynote, and other GUI apps?

pbcopy?

https://ss64.com/mac/pbcopy.html

1 Like

Yes, this seems to be somewhat related to what I’m looking for, but the real solution I would prefer is simply a keyboard shortcut, or maybe several keyboard shortcuts that must be pressed one after another, like the “Control-U, Control-Y” method mentioned above. Something that just works, out-of-the-box.

You could use something like keyboard maestro to set your own keyboard shortcut to run a command. The following command will copy the last command entered in the terminal to the clipboard:

echo !-1 | pbcopy

to copy arbitrary text from terminal to the keyboard:

echo Foo | pbcopy

Alternatively, use a different terminal app. I highly recommend iTerm2 which has copy mode that lets you select and copy text from the keyboard.

2 Likes

Just found this: Zsh copy & paste system wide for OS X, like in emacs · GitHub

Works for me.