基本原理就是读取vim或者tmux的buffer到系统剪贴板

VIM

具体原理查看这篇文章:https://segmentfault.com/a/1190000022760723

在确认vim支持剪贴板之后,在vim的配置文件中写入如下配置

1
set clipboard=unnamedplus " 将+寄存器与系统剪贴板相关联

之后便可以将默认未命名寄存器和系统剪贴板关联起来。

TMUX

关于tmux与剪贴板交互,网上给出的教程大都如下:

1
2
3
4
# buffer缓存复制到Linux系统粘贴板
bind C-c run " tmux save-buffer - | xclip -i -sel clipboard"
# Linux系统粘贴板内容复制到会话
bind C-v run " tmux set-buffer \"$(xclip -o -sel clipboard)\"; tmux paste-buffer"

在我所使用的的tmux版本下(tmux 3.0a)是无法工作的,可以尝试以下配置

1
2
bind C-y run -b "tmux save-buffer - | xclip -selection clipboard"
bind C-p run -b "xclip -o | tmux load-buffer - ; tmux paste-buffer"

与上面主要不同的是加上了 -b 选项,通过man tmux,我们可以找到下面说明:

1
2
3
4
5
6
7
8
run-shell [-b] [-t target-pane] shell-command
(alias: run)
Execute shell-command in the background without creating a window. Before
being executed, shell-command is expanded using the rules specified in the
FORMATS section. With -b, the command is run in the background. After it
finishes, any output to stdout is displayed in copy mode (in the pane
specified by -t or the current pane if omitted). If the command doesn't
return success, the exit status is also displayed.

其中有说到使用-b选项会让命令在后台执行,加-b选项是为了使shell命令不会阻塞当前session。
据我观察,在shell中执行上面的命令,在执行完成后xclip的进程不会自动退出,可能是这个原因导致如果不加-b选项就会导致tmux被阻塞。