V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
qanniu
V2EX  ›  macOS

mac 下 ssh Linux 后 vim 文字如何复制到本地

  •  
  •   qanniu · 2021-08-21 18:44:10 +08:00 · 2670 次点击
    这是一个创建于 950 天前的主题,其中的信息可能已经有所发展或是发生改变。

    似乎没有什么优美的方法。 目前用 option 键 workaround, 大家有更优美的方法吗?

    15 条回复    2021-09-22 17:20:12 +08:00
    Tiande
        1
    Tiande  
       2021-08-21 18:47:08 +08:00
    复制到临时文档里,再 sftp 出来
    Tiande
        2
    Tiande  
       2021-08-21 18:47:59 +08:00
    用带编辑功能的 sftp 工具应该是比较省事的方案
    creanme
        3
    creanme  
       2021-08-21 18:56:19 +08:00
    先保存退出再 cat 然后复制到本地剪切板?
    AllenHua
        4
    AllenHua  
       2021-08-21 19:08:24 +08:00
    我日常的习惯就是上面三楼的结合体

    行数不多的话直接 cat 出来 然后选中 Command + C 拷贝

    行数很多的话 你可以标准输出到另一个文件然后通过 scp samba 或者 ftp sftp 拷贝到本机

    cat xxx.conf > /mnt/disk/path/abc.txt

    然后再将 abc.txt 移动到本地
    hronro
        5
    hronro  
       2021-08-21 19:20:11 +08:00
    用 Alacritty 的 Vim mode 复制
    lucifer9
        6
    lucifer9  
       2021-08-21 20:03:52 +08:00 via iPhone   ❤️ 1
    osc52 ?
    Kobayashi
        7
    Kobayashi  
       2021-08-21 20:06:31 +08:00 via Android
    - osc52 https://github.com/ojroques/vim-oscyank
    - 或者开 SSH X Forwarding 同步剪贴板,需要 vim 开启 clipboard 特性
    IgniteWhite
        8
    IgniteWhite  
       2021-08-21 20:07:16 +08:00
    我的方案。

    Vim 和剪贴板有关的配置
    https://github.com/g6ai/dotfiles/blob/902cce75199f33cfed68e68bce141d716827f4e8/vim/vimrc#L154-L177

    tmux 通过 SSH 的复制(我写了个 wiki,里面有配置细节和配置文件链接)
    https://github.com/g6ai/dotfiles/wiki/tmux.conf#copy-over-ssh

    比较正统(优雅)的方式是通过 SSH 本身来 forward 内容。我在 wiki 这一节写了细节:
    https://github.com/g6ai/dotfiles/wiki/tmux.conf#helper-scripts
    大概就是通过设置合适的$DISPLAY 环境变量,让 SSH 搞清楚正确的 X session 。在 tmux 里这一步需要手动做。用 tmux 不好的地方是设置起来稍微麻烦(不过你用我的配置就一劳永逸,而且用任何 terminal 都一样)。在 tmux 之外通过 SSH 复制内容,取决于你的 terminal emulator 对于 X11 forwarding 是否有好的支持,比如楼上提到的 Alacritty 有自己的方法。

    大前提是你用的是 X11 。如果是用的比较新的 Wayland,那一切原理都不一样了。
    IgniteWhite
        9
    IgniteWhite  
       2021-08-21 20:08:08 +08:00
    @IgniteWhite 补充:在 ssh config 里面要开启 X11 Forwarding
    jdhao
        10
    jdhao  
       2021-08-21 20:30:33 +08:00 via Android
    @Kobayashi 用 osc52 应该是最方便的了,不过要注意你的 terminal 是否支持,kitty 和 iterm2 应该是支持的
    archean
        11
    archean  
       2021-08-22 06:25:13 +08:00 via iPhone
    难道不是…直接拿鼠标选中,Command+C 就完了吗?
    AndyZhuAZ
        12
    AndyZhuAZ  
       2021-08-22 16:27:55 +08:00
    复制粘贴不是可以用的吗?
    gesse
        13
    gesse  
       2021-08-24 15:53:28 +08:00
    cat <<EOF >>~/.vimrc

    if has('mouse')
    set mouse=r
    endif

    EOF
    qanniu
        14
    qanniu  
    OP
       2021-08-24 19:51:02 +08:00
    @gesse
    谢谢, 这是我个人觉得最无痕的方法.
    但没明白` r for hit-enter and more-prompt prompt` 含义

    ```
    'mouse' string (default "", "a" for GUI, MS-DOS and Win32,
    set to "a" in defaults.vim)
    global
    {not in Vi}
    Enable the use of the mouse. Only works for certain terminals
    (xterm, MS-DOS, Win32 win32-mouse, QNX pterm, *BSD console with
    sysmouse and Linux console with gpm). For using the mouse in the
    GUI, see gui-mouse.
    The mouse can be enabled for different modes:
    n Normal mode and Terminal modes
    v Visual mode
    i Insert mode
    c Command-line mode
    h all previous modes when editing a help file
    a all previous modes
    r for hit-enter and more-prompt prompt
    Normally you would enable the mouse in all four modes with:
    :set mouse=a
    When the mouse is not enabled, the GUI will still use the mouse for
    modeless selection. This doesn't move the text cursor.
    See mouse-using. Also see 'clipboard'.
    Note: When enabling the mouse in a terminal, copy/paste will use the
    "* register if there is access to an X-server. The xterm handling of
    the mouse buttons can still be used by keeping the shift key pressed.
    Also see the 'clipboard' option.
    ```
    aec4d
        15
    aec4d  
       2021-09-22 17:20:12 +08:00
    OSC52 应该是我用过最好的方法

    远程
    sudo dd of=/usr/local/bin/osc52 <<'EOF'
    #!/bin/bash
    printf "\033]52;c;$(base64 | tr -d '\r\n')\a"
    EOF
    sudo chmod +x /usr/local/bin/osc52

    本地对 iterm2 和 tmux 开启 osc52 支持
    set -g set-clipboard on
    远程 cat file | osc52 就可以了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3323 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 13:30 · PVG 21:30 · LAX 06:30 · JFK 09:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.