Neovim使用系统的剪切板

一直使用Neovim作为日常办公的主力,偶尔也使用一下系统的剪切板从外部粘贴文本,在X11时直接使用鼠标就能直接粘贴到Neovim中,但是系统升级到wayland之后,每次点鼠标粘贴时都会弹出一个确认的对话框,点击之后才粘贴上!这很影响办公效率,于是今天决定认真对待一下这个问题。启动Neovim,然后输出命令:h clipboard就可以看到全部详细信息了,为了提高效率,此处记录关键内容。

Vim与Neovim使用系统剪切板的不同

Nvim has no direct connection to the system clipboard. Instead it depends on a |provider| which transparently uses shell commands to communicate with the system clipboard or any other clipboard "backend".

设置Neovim所有操作使用剪切板

要始终将剪贴板用于所有操作(而不是显式地与“+”和/或“*”寄存器交互, 需要设置init.vim中加入:

1
set clipboard+=unnamedplus

安装剪切板依赖

The presence of a working clipboard tool implicitly enables the '+' and "*" registers. Nvim looks for these clipboard tools, in order of priority:

  • |g:clipboard| (unless unset or false)
  • pbcopy, pbpaste (macOS)
  • wl-copy, wl-paste (if $WAYLAND_DISPLAY is set)
  • waycopy, waypaste (if $WAYLAND_DISPLAY is set)
  • xsel (if $DISPLAY is set)
  • xclip (if $DISPLAY is set)
  • lemonade (for SSH) https://github.com/pocke/lemonade
  • doitclient (for SSH) https://www.chiark.greenend.org.uk/~sgtatham/doit/
  • win32yank (Windows)
  • termux (via termux-clipboard-set, termux-clipboard-set)
  • tmux (if $TMUX is set)

由于我的电脑同时支持waylandx11, 所以我直接安装了wl-clipboardxclip, 其中wl-clipboard提供命令wl-copywl-paste

在Neovim的粘贴快捷键

"Paste" is a separate concept from |clipboard|: paste means "dump a bunch of text to the editor", whereas clipboard provides features like |quote+| to get and set the OS clipboard directly. For example, middle-click or CTRL-SHIFT-v (macOS: CMD-v) in your terminal is "paste", not "clipboard": the terminal application (Nvim) just gets a stream of text, it does not interact with the clipboard directly.

能过这段话我们可知,在Linux下,使用命令CTRL-SHIFT-v就可以直接将剪切板中的内容粘贴到Neovim中了,在macOS中使用命令CMD-v.