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

2021-08-21 18:44:10 +08:00
 qanniu

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

2710 次点击
所在节点    macOS
15 条回复
Tiande
2021-08-21 18:47:08 +08:00
复制到临时文档里,再 sftp 出来
Tiande
2021-08-21 18:47:59 +08:00
用带编辑功能的 sftp 工具应该是比较省事的方案
creanme
2021-08-21 18:56:19 +08:00
先保存退出再 cat 然后复制到本地剪切板?
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
2021-08-21 19:20:11 +08:00
用 Alacritty 的 Vim mode 复制
lucifer9
2021-08-21 20:03:52 +08:00
osc52 ?
Kobayashi
2021-08-21 20:06:31 +08:00
- osc52 https://github.com/ojroques/vim-oscyank
- 或者开 SSH X Forwarding 同步剪贴板,需要 vim 开启 clipboard 特性
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
2021-08-21 20:08:08 +08:00
@IgniteWhite 补充:在 ssh config 里面要开启 X11 Forwarding
jdhao
2021-08-21 20:30:33 +08:00
@Kobayashi 用 osc52 应该是最方便的了,不过要注意你的 terminal 是否支持,kitty 和 iterm2 应该是支持的
archean
2021-08-22 06:25:13 +08:00
难道不是…直接拿鼠标选中,Command+C 就完了吗?
AndyZhuAZ
2021-08-22 16:27:55 +08:00
复制粘贴不是可以用的吗?
gesse
2021-08-24 15:53:28 +08:00
cat <<EOF >>~/.vimrc

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

EOF
qanniu
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
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 就可以了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/797205

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX