打通Neovim与系统剪切板

1. 如何确认vim和系统剪贴板是否互通

(1) reg 查看寄存器

  (2) 查看+ 和 * 2个寄存器是否有系统剪贴板的内容,如果有表示互通,如果没有,表示没有互通

 

2. 打通剪贴板

   (3) 没有互通,安装剪贴板工具 xclip

        sudo apt install xclip -y

   (4) 执行第2部的步骤,检查剪贴板状况

 

 

neovim中关于剪切板配置的说明:

Clipboard integration 			      *provider-clipboard* *clipboard*

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".

To ALWAYS use the clipboard for ALL operations (instead of interacting with
the '+' and/or '*' registers explicitly): >
    set clipboard+=unnamedplus

See 'clipboard' for details and options.

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

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

								 *g:clipboard*
To configure a custom clipboard tool, set g:clipboard to a dictionary.
For example this configuration integrates the tmux clipboard: >

    let g:clipboard = {
          \   'name': 'myClipboard',
          \   'copy': {
          \      '+': 'tmux load-buffer -',
          \      '*': 'tmux load-buffer -',
          \    },
          \   'paste': {
          \      '+': 'tmux save-buffer -',
          \      '*': 'tmux save-buffer -',
          \   },
          \   'cache_enabled': 1,
          \ }

If "cache_enabled" is |TRUE| then when a selection is copied Nvim will cache
the selection until the copy command process dies. When pasting, if the copy
process has not died the cached selection is applied.

g:clipboard can also use functions (see |lambda|) instead of strings.
For example this configuration uses the g:foo variable as a fake clipboard: >

    let g:clipboard = {
          \   'name': 'myClipboard',
          \   'copy': {
          \      '+': {lines, regtype -> extend(g:, {'foo': [lines, regtype]}) },
          \      '*': {lines, regtype -> extend(g:, {'foo': [lines, regtype]}) },
          \    },
          \   'paste': {
          \      '+': {-> get(g:, 'foo', [])},
          \      '*': {-> get(g:, 'foo', [])},
          \   },
          \ }

The "copy" function stores a list of lines and the register type. The "paste"
function returns the clipboard as a `[lines, regtype]` list, where `lines` is
a list of lines and `regtype` is a register type conforming to |setreg()|.

 

 

所以我们直接使用自定义剪切板就ok,我们让+和*寄存器都使用clipboard-provider 脚本来配置。

在neovim的配置中增加以下代码:

neovim的配置相对较多,如果是spacevim中,可以加到~/.config/nvim/main.vim中
if executable('clipboard-provider')
  let g:clipboard = {
          \ 'name': 'myClipboard',
          \     'copy': {
          \         '+': 'clipboard-provider copy',
          \         '*': 'clipboard-provider copy',
          \     },
          \     'paste': {
          \         '+': 'clipboard-provider paste',
          \         '*': 'clipboard-provider paste',
          \     },
          \ }
endif

 

此时,重启nvim,应该已经大功告成了。复制时,通过 "+y 命令复制,就可以复制到剪切板。如果不行,可以输入 :reg ,看相关代码是否插入到 + 寄存器中。如果在寄存器中,同时你的clipboard-provider 脚本没问题的话,应该是可以成功复制的。

 

原始文章:

https://zhuanlan.zhihu.com/p/419472307

声明:

  文章转载只为资料不丢失

posted @ 2022-01-20 09:48  jiftle  阅读(4776)  评论(1编辑  收藏  举报