问题尽管有一个答案被接受但未解决:我们正努力让Jonah的代码正常运作。

问题:将(1)的代码更改为(2)

我知道主题。我希望能够在Screen

中运行以下代码

代码(1)

cat ~/.vimrc | pbcopy                   (1)

代码(2)

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

我尝试解决问题: 将以下代码放到.zshrc

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

我得到了

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

如何在屏幕内使用pbcopy?

有帮助吗?

解决方案

好吧,这是一个棘手的答案,但它也是一个棘手的问题,所以至少它们是匹配的。您可以使用 mkfifo 创建命名管道,然后设置一个无限循环,从命名管道读取文件并将它们传递给 pbcopy (或 xsel xclip 等)。

1。在不在屏幕会话中的终端中(仅运行一次):

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

您可能希望将其转换为shell脚本(这可能应该更强大)

#!/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

你可以将命名为pbcopy_server.sh ,制作可执行文件( chmod a + x pbcopy_server.sh )并将其放在你的路径中,这样你就可以说首次启动机器时nohup pbcopy_server.sh&

2. 在任何其他终端(包括屏幕会话中的终端)中,您现在可以使用cat文件(或将程序输出重定向到/tmp/pbcopy.pipe,文本将显示在剪贴板中。

cat file > /tmp/pbcopy.pipe

df -h > /tmp/pbcopy.pipe

3。要让它看起来像是在调用真正的 pbcopy ,你可以使用一些东西来执行 /tmp/pbcopy.pipe 给你。

3a。使用 zsh 功能:

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

3b。或者创建一个名为 pbcopy 的Perl脚本,并将其放在 PATH 中早于 / usr /的目录中仓

#!/usr/bin/perl

use strict;
use warnings;

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

print $out 

好吧,这是一个棘手的答案,但它也是一个棘手的问题,所以至少它们是匹配的。您可以使用 mkfifo 创建命名管道,然后设置一个无限循环,从命名管道读取文件并将它们传递给 pbcopy (或 xsel xclip 等)。

1。在不在屏幕会话中的终端中(仅运行一次):

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

您可能希望将其转换为shell脚本(这可能应该更强大)

#!/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

你可以将命名为pbcopy_server.sh ,制作可执行文件( chmod a + x pbcopy_server.sh )并将其放在你的路径中,这样你就可以说首次启动机器时nohup pbcopy_server.sh&

2. 在任何其他终端(包括屏幕会话中的终端)中,您现在可以使用cat文件(或将程序输出重定向到/tmp/pbcopy.pipe,文本将显示在剪贴板中。

cat file > /tmp/pbcopy.pipe

df -h > /tmp/pbcopy.pipe

3。要让它看起来像是在调用真正的 pbcopy ,你可以使用一些东西来执行 /tmp/pbcopy.pipe 给你。

3a。使用 zsh 功能:

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

3b。或者创建一个名为 pbcopy 的Perl脚本,并将其放在 PATH 中早于 / usr /的目录中仓

<*> while <>;

其他提示

http://www.samsarin.com/blog/2008/10/18/copying-gnu-screen-buffer-to-leopard-clipboard/

在评论中,Andrew Wason提供了这种解决方案来复制屏幕缓冲区:

.screenrc中的代码

# binds C-a b to copy the contents of your last screen copy to the MacOSX pasteboard
bind b eval "writebuf /tmp/screen-pbcopy" "exec /usr/bin/osascript -e 'tell application \"System Events\"' -e 'set the clipboard to (read posix file \"/tmp/screen-pbcopy\" as text)' -e 'end tell'"

同样使用osascript,这是一个模拟屏幕内pbcopy行为的bash脚本。欢迎对此脚本进行改进:

将此代码保存为路径中的某个bash脚本,例如:〜/ bin / pbcopyScreen.bash

#!/bin/bash

# saves all standard input to a file
cat > /tmp/screen_pbcopy_kludge_buffer

# uses osascript to set the MacOSX pastebaord to the contents of the file
/usr/bin/osascript -e 'tell application "System Events"' -e 'set the clipboard to (read posix file "/tmp/screen_pbcopy_kludge_buffer" as text)' -e 'end tell'

rm /tmp/screen_pbcopy_kludge_buffer

这似乎是在Snow Leopard版本的GNU Screen中修复的,即使它保持相同的版本号4.00.03(FAU)2006年10月23日。

或者,您可以更新到屏幕版本4.01:

git clone git://git.savannah.gnu.org/screen.git
scroll top