Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
665 views
in Technique[技术] by (71.8m points)

macos - Unable to have pbcopy -clipboard inside Screen

Problem Not solved although one answer was accepted: We are working to get Jonah's code to work.

Problem: to change the code of (1) to (2)

I know the thread. I want to be able to run the following code inside Screen

Code (1)

cat ~/.vimrc | pbcopy                   (1)

Code (2)

cat ~/.vimrc > /tmp/pbcopy.pipe         (2)

My attempt to solve the problem: to put the following code to .zshrc

function pbcopy() { "(cat "$1")"  > /tmp/pbcopy.pipe } 

I get

cat masi | pbcopy          
pbcopy: command not found: (cat "")
cat: masi: No such file or directory

How can you use pbcopy inside Screen?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Alright, this is a screwy answer, but it is also a screwy question, so at least they match. You can create a named pipe with mkfifo, and then setup an infinite loop that reads files from the named pipe and pipes them to pbcopy (or xsel, xclip, etc.).

1. In a terminal which is NOT in a screen session (run this only once):

/usr/bin/mkfifo /tmp/pbcopy.pipe
while true; do /bin/cat /tmp/pbcopy.pipe | /usr/bin/pbcopy; done

You may want to turn this into a shell script like (this probably should be more robust)

#!/bin/bash

if [[ -e /tmp/pbcopy.pipe ]]; then
    echo "it looks like I am already running"
    echo "remove /tmp/pbcopy.pipe if you are certain I am not"
    exit 1
fi

while true; do
    /bin/cat /tmp/pbcopy.pipe | /usr/bin/pbcopy
done

which you can name pbcopy_server.sh, make executable (chmod a+x pbcopy_server.sh) and put somewhere in your path, so you can say nohup pbcopy_server.sh & when you first start your machine.

2. In any other terminal (including those in screen sessions) you can now cat files (or redirect output of programs into /tmp/pbcopy.pipe and the text will appear in the clipboard.

cat file > /tmp/pbcopy.pipe

df -h > /tmp/pbcopy.pipe

3. To make it look like you are calling the real pbcopy you can use something to do the cat'ing to /tmp/pbcopy.pipe for you.

3a. Use a zsh function:

function pbcopy() { cat > /tmp/pbcopy.pipe }

3b. Or create a Perl script named pbcopy and put it in a directory earlier in your PATH than /usr/bin:

#!/usr/bin/perl

use strict;
use warnings;

open my $out, ">", "/tmp/pbcopy.pipe"
   or die "could not open pipe to pbcopy: $!
";

print $out $_ while <>;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...