V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
hanxiV2EX
V2EX  ›  分享创造

再次推荐共享粘贴板 Lemonade

  •  
  •   hanxiV2EX ·
    hanxi · 2019-05-16 11:19:32 +08:00 · 2492 次点击
    这是一个创建于 1779 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1. lemonade 使用场景

    项目地址: https://github.com/hanxi/lemonade

    Windows/Linux/MacOS 桌面环境使用 SSH 远程连接服务器开发。无需鼠标 选择 /拷贝 /粘贴 文本。

    比如需要在 Vim 中拷贝一段文本,然后粘贴到桌面软件(QQ?微信?)。 比如需要拷贝的文本超出了一屏幕,用鼠标就不方便了。而且还需要把 Vim 行号临时取消才能用鼠标选择拷贝。

    lemonade 分为 client 和 server。 桌面环境(本地)运行 server 端,远程环境运行 client 端。

    2. 使用示例

    下面配置环境以 Windows 下 Putty 连接 Linux 服务器开发为例。

    lemonade 下载地址 https://github.com/hanxi/lemonade/releases/tag/v2.0.0-pre

    2.1 在 Windows 上配置 server 端

    下载对应版本的 Windows 包,32 位 /64 位。解压后得到 lemonade.exe 文件

    在 cmd.exe 里执行下面命令

    lemonade.exe server --allow="127.0.0.1,::1" --port=2489 --line-ending="crlf"
    

    这行命令的意思是用 server 方式启动 lemonade, 只允许本机连接,监听端口为 2489,换行符号为 '\r\n'

    • 设置换行符可以解决 Linux 复制一段文本到 Windows 的 notepad 没有换行的问题。

    • 只允许本地连接会比较安全,然后使用 SSH 把本地的 2489 端口映射到 Linux 的 2489 端口。

    • 可以自己写 start.bat 文件,然后丢到系统启动项里面。后期有空会加个 system tray,对用户会比较友好。

    • 配置 Putty 端口转发:

      Connection -> SSH -> Tunnels

      Add new forwarded port
      Source port 2489
      Destination localhost:2489
      Remote
      Auto
      

      配出的结果这样显示的:R2489 localhost:2489

    2.2 在 Linux 上配置 client 端

    下载对应版本的 Linux 包,拷贝到 /usr/bin/ 或者其他 PATH 路径

    新建配置文件 ~/.config/lemonade.toml

    填入以下内容

    port = 2489
    host = '127.0.0.1'
    line-ending = 'lf'
    trans-loopback = true
    trans-localfile = true
    

    步骤 2.1 已经配置好了端口映射,所以这里填的 host 为 127.0.0.1

    测试是否配置成功:

    1. 在 Linux 上输入命令
    echo "test" | lemonade copy
    
    1. 在 Windows 上粘贴

    2.3 配置 NeoVim 使用 lemonade

    let g:clipboard = {
                \'copy': { '+': 'lemonade copy', '*': 'lemonade copy' },
                \'paste': { '+': 'lemonade paste', '*': 'lemonade paste' },
                \'name': 'lemonade',
                \}
    

    这样就可以从 Vim 中用 y 拷贝文本到 Windows 了,也可以用 p 直接粘贴 Windows 上拷贝的文本了。

    2.4 配置 Tmux 使用 lemonade

    setw -g mode-keys vi
    bind-key -T copy-mode-vi v send-keys -X begin-selection
    bind-key -T copy-mode-vi y send -X copy-pipe-and-cancel "~/.local/bin/lemonade copy"
    bind-key ] run-shell "~/.local/bin/lemonade paste | tmux load-buffer -" \; paste-buffer ;
    

    这样就可以从 Tmux 中用 y 拷贝文本到 Windows 了,也可以用 Ctrl + b + ] 直接粘贴 Windows 上拷贝的文本了。

    3. 高级功能

    3.1 open url

    在 Linux 打开 Windows 上的浏览器,并进入到指定的网址。比如在 Linux 上输入下面命令:

    lemonade open https://github.com/hanxi/lemonade
    

    这个功能配合 Vim 插件 iamcco/markdown-preview.nvim 非常好用,编辑 Markdown 实时预览。Vim 里这样配置即可:

    "{markdown-preview
    let g:mkdp_open_to_the_world = 1
    let g:mkdp_open_ip = '192.168.29.251'
    let g:mkdp_port = 6060
    function! g:Open_browser(url)
        silent exe '!lemonade open 'a:url
    endfunction
    

    当然,如果 Linux 的 ip 是不固定的,也可以用 Putty 的端口映射方法。把 Windows 的 6060 映射到 Linux 的 6060.

    L6060 localhost:6060

    vim 则需要这样配置

    "{markdown-preview
    let g:mkdp_open_to_the_world = 1
    let g:mkdp_open_ip = 'localhost'
    let g:mkdp_port = 6060
    function! g:Open_browser(url)
        silent exe '!lemonade open 'a:url
    endfunction
    

    3.2 upload file and open

    上传 Linux 上的文件到 Windows,并用浏览器打开。

    lemonade open ./test.html
    

    为何又一次推荐?

    之前推荐过一次 https://github.com/hanxi/blog/issues/17

    因为最近我重写了 RPC 协议,采用 HTTP REST API。不安装 lemonade 客户端也能用 curlnc 实现,比如就这么简单的实现了 client 的 copypaste 功能。

    #!/bin/sh
    
    if [ $1 == 'copy' ]; then
        curl -X POST --data "$2" http://127.0.0.1:2489/copy
    fi
    
    if [ $1 == 'paste' ]; then
        curl http://127.0.0.1:2489/paste
    fi
    

    主要原因还是这工具太好用了,摆脱鼠标的利器。

    来源: https://blog.hanxi.info/?p=26

    第 1 条附言  ·  2019-05-16 19:09:12 +08:00
    第 2 条附言  ·  2019-05-19 09:59:19 +08:00

    不知道这里有没有人试用过,我又给它加了点东西,System tray.

    • Windows 右下角图标
    • 可设置开机自启
    • 可打开配置文件配置

    这里可以下载。运行在 Linux 里的客户端可以不用更新,没有改动协议,上个版本的也能用。

    https://github.com/hanxi/lemonade/releases/tag/v2.0.1-pre

    7 条回复    2019-05-17 18:39:46 +08:00
    xml123
        1
    xml123  
       2019-05-16 15:45:21 +08:00
    vim 不是可以复制到剪贴板吗,为什么要用鼠标
    omph
        2
    omph  
       2019-05-16 16:35:55 +08:00
    讲解的挺凌乱,有拓扑图吗?
    hanxiV2EX
        4
    hanxiV2EX  
    OP
       2019-05-16 19:05:47 +08:00 via Android
    @xml123
    在 windows 下用 putty 远程连接 linux 开发,linux 里的 vim 的粘贴板跟 windows 下的共享。
    Thinkerous
        5
    Thinkerous  
       2019-05-17 00:41:38 +08:00
    Linux 命令行的时候这个真的是个痛点
    www5070504
        6
    www5070504  
       2019-05-17 10:31:26 +08:00
    弱问一下 支持 xshell 么
    hanxiV2EX
        7
    hanxiV2EX  
    OP
       2019-05-17 18:39:46 +08:00 via Android   ❤️ 1
    @www5070504 支持的,不配置端口转发也能用。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5256 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 07:24 · PVG 15:24 · LAX 00:24 · JFK 03:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.