Automagically Copy Command Output to Clipboard?

1,481 views
Skip to first unread message

BeeRich33

unread,
Nov 19, 2014, 12:11:01 PM11/19/14
to iterm2-...@googlegroups.com
Hiya folks.

Just wondering if there is a way to send the output to the clipboard.  Hopefully a command key that sets the capture on for the next return only.  That would be fantastic.  

Cheers

Michael Garrett

unread,
Dec 2, 2014, 3:11:10 PM12/2/14
to iterm2-...@googlegroups.com
I have no idea how hard that would be to implement, but I would definitely use that feature if the developers added it.  +1


--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steve King

unread,
Dec 2, 2014, 4:52:18 PM12/2/14
to iterm2-...@googlegroups.com
On 2014-11-19 12:11 , BeeRich33 wrote:
Hiya folks.

Just wondering if there is a way to send the output to the clipboard.  Hopefully a command key that sets the capture on for the next return only.  That would be fantastic.

Can you give an example? I'm a little unclear what you're asking for.

Maybe 'pbcopy' would work for you?  It reads text from stdin and puts it on the clipboard.  For example, "ls | pbcopy" puts the output of the 'ls' command onto the clipboard. There's also a 'pbpaste' to dump the contents of the clipboard to stdout.

--
Steve King | Principal Software Engineer
Arbor Networks

George Nachman

unread,
Dec 2, 2014, 7:02:59 PM12/2/14
to iterm2-...@googlegroups.com
If you use the nightly build and install shell integration, then Cmd-shift-A will select the output of the last command.

--

maurizio....@gmail.com

unread,
Nov 14, 2016, 10:28:00 AM11/14/16
to iterm2-discuss
Resurrecting this old thread, but the proposed solution aren't that useful in my specific case.

I looked all over the internet.

The pbcopy works if pbcopy is installed on the remote machine

the xclip solution, same thing, works if the xclip is installed

the reverse ssh to run pbcopy works only if there is a direct connection between the remote machine and my local machine, which isn't the case, we go through two hosts and vpn + firewalls.

all i would need would be a plugin that grabs everything from the stdout once i cat a file.

iTerm is able to display the remote file with cat, but sometimes it might be just bigger than the whole screen history. So it would be possible automatically copy the content as it is being displayed, then append the data in a virtual buffer that is then used to populate the local clipboard at the end of the command.

That would greatly help for copying small files instead of SCPing the file across several hosts... and then cleaning up the various steps...

Thanks

Steve King

unread,
Nov 14, 2016, 11:19:05 AM11/14/16
to iterm2-...@googlegroups.com
On 2016-11-14 10:28, maurizio....@gmail.com wrote:
iTerm is able to display the remote file with cat, but sometimes it might be just bigger than the whole screen history. So it would be possible automatically copy the content as it is being displayed, then append the data in a virtual buffer that is then used to populate the local clipboard at the end of the command.

A couple of suggestions.  First there's the logging function under "Session -> Log". This isn't quite what you asked for but it might be what you actually want.  It captures all output to a file.  (It would be kind of cool if you could log to the clipboard, though!)

Second, it's possible to set up something with triggers and coprocesses. If you google for "iterm coprocess zmodem" you can find some examples using zmodem to transfer files from a remote system. Since it's using the regular terminal stdio channel it doesn't matter how many hops or VPN logins are between you and your remote host.  You do have to have the zmodem commands installed though. (Typically 'sz' on the sending side, 'rz' on the receiving side.)

I don't have the luxury of installing software on my remote hosts, and zmodem isn't available. Instead I wrote my own cheap alternative. I set up the following bash function on my remote host:

sendfile ()
{
    echo '-*-{{SENDFILE}}-*-';
    echo "{\"version\": 1.0, \"host\": \"`hostname`\"}";
    env COPYFILE_DISABLE=true tar czf - "$@" 2> /dev/null | base64;
    echo '-*-{{DONE}}-*-'
}

What this does is sends the string "-*-{{SENDFILE}}-*-", which is a unique string I can use to trigger a coprocess.  Then it sends a line of JSON with the protocol version and the name of the sending host.  After that it runs 'tar' to bundle up all the files I specified on the command line, filters the result through base64, and prints it to the terminal.

On my Mac I set up a trigger in iTerm to run a coprocess when it sees that "-*-{{SENDFILE}}-*-" string.  The coprocess is a python script which is expecting a line of JSON followed by a base64-encoded tar file. Once the tar file is received the coprocess decodes and unpacks it to my ~/Downloads directory.  It could easily be modified to write files to the clipboard if desired.  I'm not completely happy with it (it bogs down on large files, I'm not sure why) but I'll post it to GitHub when I get a chance.  (The repo is at https://github.com/SteveKing/sendfile but it's currently empty.)

--
Steve King
st...@narbat.com

George Nachman

unread,
Nov 17, 2016, 3:19:09 PM11/17/16
to iterm2-...@googlegroups.com
The coprocess solution is very clever :)

You can also send it straight to the pasteboard with this:

printf "\033]1337;CopyToClipboard=\001"
cat giantfile
printf "\033]1337;EndCopy\001"

Make sure Prefs>General>Applications in terminal may access clipboard is ON and Prefs>Advanced>Disable potentially insecure escape sequences is OFF.

Another option would be the it2dl script that's installed alongside shell integration. It downloads instead of copying, though.



--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages