Press "Enter" to skip to content

使用OSC52实现iTerm2远程pbcopy

最高效的码字方式,一定有一个特点,那就是手指离开键盘的次数尽可能的少。 并且手指码字的时候的在键盘的跨度尽可能的短, 这也是我认识不少老牌程序员用Vim也不用ESC, 而是使用ctrl+[, 甚至还有这:

从而避免手指在键盘上的跨越幅度太大。

我一直用MacBook做工作用机,也一直用iTerm2来登陆远程服务器开发,因为PHP是一个比较巨大的工程,编译速度很慢,自己的笔记本再高配,也是做不到make -j不卡死的。

言归正传, 在mac上有一个很有用的命令pbcopy, 它能让我们避免去使用鼠标选择文本,复制到剪贴板。从而让我们的双手可以一直在键盘上,但很可惜的是,pbcopy只能在本机使用,而我大量的时间都是使用远程机。

最近发现了一个很有趣的OSC 52,也就是ANSI escape code中的 ESC ] (Operating System Command), 中的52, 它提供了访问访问本地剪贴板的能力。

它的形式为:

echo -e "\e]52;c;$(base64 <<< php)\a"

也就是,以\e]52;c;开头,然后是base64 encode后的内容,以\a结尾, 如果terminal支持,那么上面例子中的文字“php”就会进入到你的剪贴板了,你就可以使用command + V粘贴了。

对于我常用的iTerm2来说, 只要打开这个选项即可:

有了这个,那么我们就可以很容写出一个支持远程服务器的pbcopy了, 以PHP为例(
你也可以直接下载:OSC52.php):

#!/bin/env php
<?php
$data = trim(file_get_contents("php://stdin"));
echo "\033]52;c;", base64_encode($data), "\007";

然后给这个文件增加执行权限, 重命名成pbcopy,试试:

echo "osc52 codes" | ./pbcopy

然后command+v粘贴下试试?

这下就香很多了,避免了需要用鼠标来选择复制一些text。 让手可以一直在键盘上,极大提升生产力!
然而,我现在开始用iPad办公了, 目前为止还没有发现iPad下的terminal App有支持OSC 52的。 大家要是发现哪个App支持,一定留言告诉我哈。 :)

19 Comments

  1. Tattvam Goa
    Tattvam Goa June 24, 2023

    Welcome to Tattvam on the Beach, One of the top Yoga Resorts in Goa , your gateway to a blissful seaside retreat. Located in the picturesque coastal town, invites you to explore a world where luxury, tranquility, and rejuvenation converge. With its pristine beaches, stunning ocean views, and a serene atmosphere, Tattvam on the Beach is a sanctuary designed to awaken your senses and nourish your soul.

  2. gudlu resort
    gudlu resort June 24, 2023

    Welcome to Gudlu Resort, a hidden gem nestled amidst the picturesque hills of Mudigere in Chikmagalur. If you are looking for a Chikmagalur resorts , Gudlu Resort is the place to be. Surrounded by acres of lush greenery, Gudlu Resort offers a range of luxurious accommodations that are perfect for couples, families, and groups. Each room is well-appointed with modern amenities and offers breathtaking views of the surrounding hills and valleys.

  3. tattvamretreat
    tattvamretreat June 24, 2023

    Welcome to Tattvamretreat Resort, Bangalore – a serene and tranquil wellness resorts in bangalore that offers a holistic approach to wellness through Ayurveda and Yoga. Tucked away amidst lush greenery and rolling hills, Tattvamretreat Resort is a perfect getaway for those seeking a peaceful and rejuvenating experience.

  4. Gari resort
    Gari resort June 24, 2023

    Welcome to Gari Resort, Bangalore – one of the best day outing resorts in Bangalore that offers a unique blend of luxury, comfort, and relaxation. If you are looking for the perfect escape from the hustle and bustle of city life, Gari Resort is the place for you.

  5. Godrej splendor whitefield revie Developed by Godrej splendor of Tumkur Road, Bangalore. Jindal City is a great experience including home, work, school for kids, good hospitals, entertainment venues, favorite restaurants, shopping malls, etc. Everything is preferably within the shortest distance. Godrej Splendor Whitefield consists of 3,571 smart sized residential units built on 32 acres with a variety of amenities that are close to everything you need. Godrej Splendor Whitefield offers 2 BHK, 3 BHK and 4 BHK sophisticated apartments in 15 blocks on G+30 floors.

  6. Sobha Neopolis
    Sobha Neopolis April 7, 2023

    Sobha Neopolis consists of residential apartments of various sizes. The unit area available for this project is 38 acres. This area is part of the Bannerghatta Road suburb. It consists of 1 BHK, 2 BHK and 3 BHK apartments. located in Bangalore. Locals have easy access to city facilities such as classrooms, colleges, clinics, recreation centers and parks. Inside the building, the building is specifically planned.

  7. nextvary
    nextvary August 3, 2020

    👍

  8. zhaoshuai
    zhaoshuai June 9, 2020

    鸟哥,求个微信好友位~

  9. Klusfq
    Klusfq June 6, 2020

    哈哈pbcopy和pbpaste确实好用,牛逼

  10. yisangwu
    yisangwu June 4, 2020

    laruence,
    博客里面的图片,能不能 在新窗口打开? a target=”_blank”。
    点开图片,还要回退。

  11. Jack
    Jack June 3, 2020

    我就想说一下 我还是会回来查资料的。

  12. 害
    May 22, 2020

    option+触摸板选择 方便多了

  13. 桃子
    桃子 May 22, 2020

    我实测失败,是不是不够通用?
    确定已打开:

    • 桃子
      桃子 May 22, 2020

      噢,我错了。我在远程的tmux里面测试的,失败了。直接在 shell 里面是成功的。我再研究一下。

      • laruence
        laruence May 22, 2020

        如果是tmux,你可以直接下载我的那个例子,那个里面我做了兼容

        • 桃子
          桃子 May 22, 2020

          多谢,用的例子成功了,兼容真是个麻烦事。
          PHP在容器里面,宿主机不能用,所以写了个 bash:
          #!/bin/bash

          set -euo pipefail

          t=$(cat <&0 | base64)
          if [ ! -z ${STY+x} ]; then
          echo -ne "\eP\e]52;c;$t\a\e\\"
          elif [ ! -z ${TMUX+x} ]; then
          echo -ne "\ePtmux;\e\e]52;c;$t\a\e\\"
          else
          echo -ne "\e]52;c;$t\a"
          fi

          不知道贴代码会不会格式错乱。。

  14. 刘师傅
    刘师傅 May 22, 2020

    巧了,今天在看一个老知识点的参考资料时,旁边就是鸟哥的网站链接,所以,无论十万百千里,互联网就是这么神奇,我来了!
    没啥说的,只是占个楼主而已。
    当然,我可以试试鸟哥的OSC 52

Comments are closed.