Windows wsl Linux nvim 剪切板共享

Windows wsl安装Linux neovim 剪切板共享

起因 在Windows时不时用到Linux 自己装了nvim 但是 问题来了 在Linux 需要复制粘贴内容到Windows的时候就抓狂了 于是 写了如下教程

系统信息

  • windows11
  • wsl manjaro
  • neovim 9

nvim 配置 我懒得配置 直接用现成的 spaceVim

安装 去官网看

1
https://spacevim.org/cn/

操作步骤

前提安装neovim,我装了nvim 懒得配置 直接用spacevim neovim 配置类似

1.下载添加运行权限到win32yank

需要安装unzip 如果没有自行提前安装

1
2
3
4
curl -sLo/tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.1.1/win32yank-x64.zip
unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
chmod +x /tmp/win32yank.exe
sudo mv /tmp/win32yank.exe /usr/local/bin/

2 编辑对应文件 nvim ~/.config/nvim/init.vim 放在这个call SpaceVim#begin() 方法前

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
" win32yank 剪切板 共用
set clipboard=unnamedplus

if executable('clipboard-provider')
let g:clipboard = {
name = "win32yank-wsl",
copy = {
'+' = "win32yank.exe -i --crlf",
'*' = "win32yank.exe -i --crlf"
},
paste = {
'+' = "win32yank.exe -o --crlf",
'+' = "win32yank.exe -o --crlf"
},
cache_enable = 0,
}
endif

如果是lue配置参考
nvim ~/.config/nvim/init.lua

1
2
3
4
5
6
7
8
9
10
11
12
vim.g.clipboard = {
name = "win32yank-wsl",
copy = {
["+"] = "win32yank.exe -i --crlf",
["*"] = "win32yank.exe -i --crlf"
},
paste = {
["+"] = "win32yank.exe -o --crlf",
["*"] = "win32yank.exe -o --crlf"
},
cache_enable = 0,
}

3保存退出 大功告成

注意防的位置 如果没有生效 调整位置 我的是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"=============================================================================
" init.vim --- Entry file for neovim
" Copyright (c) 2016-2023 Wang Shidong & Contributors
" Author: Wang Shidong < wsdjeg@outlook.com >
" URL: https://spacevim.org
" License: GPLv3
"=============================================================================

" set default encoding to utf-8
" Let Vim use utf-8 internally, because many scripts require this
set encoding=utf-8
scriptencoding utf-8

" Enable nocompatible
if &compatible
set nocompatible
endif

let g:_spacevim_root_dir = escape(fnamemodify(resolve(fnamemodify(expand('<sfile>'),
\ ':p:h:gs?\\?'.((has('win16') || has('win32')
\ || has('win64'))?'\':'/') . '?')), ':p:gs?[\\/]?/?'), ' ')
lockvar g:_spacevim_root_dir
if has('nvim')
let s:qtdir = split(&rtp, ',')[-1]
if s:qtdir =~# 'nvim-qt'
let &rtp = s:qtdir . ',' . g:_spacevim_root_dir . ',' . $VIMRUNTIME
else
let &rtp = g:_spacevim_root_dir . ',' . $VIMRUNTIME
endif
else
let &rtp = g:_spacevim_root_dir . ',' . $VIMRUNTIME
endif
call SpaceVim#logger#info('Loading SpaceVim from: ' . g:_spacevim_root_dir)

if has('vim_starting')
" python host
" @bug python2 error on neovim 0.6.1
" let g:loaded_python_provider = 0
if !empty($PYTHON_HOST_PROG)
let g:python_host_prog = $PYTHON_HOST_PROG
call SpaceVim#logger#info('$PYTHON_HOST_PROG is not empty, setting g:python_host_prog:' . g:python_host_prog)
endif
if !empty($PYTHON3_HOST_PROG)
let g:python3_host_prog = $PYTHON3_HOST_PROG
call SpaceVim#logger#info('$PYTHON3_HOST_PROG is not empty, setting g:python3_host_prog:' . g:python3_host_prog)
if !has('nvim')
\ && (has('win16') || has('win32') || has('win64'))
\ && exists('&pythonthreedll')
\ && exists('&pythonthreehome')
let &pythonthreedll = get(split(globpath(fnamemodify($PYTHON3_HOST_PROG, ':h'), 'python*.dll'), '\n'), -1, '')
call SpaceVim#logger#info('init &pythonthreedll:' . &pythonthreedll)
let &pythonthreehome = fnamemodify($PYTHON3_HOST_PROG, ':h')
call SpaceVim#logger#info('init &pythonthreehome:' . &pythonthreehome)
endif
endif
endif

" win32yank 剪切板 共用
set clipboard=unnamedplus

if executable('clipboard-provider')
let g:clipboard = {
name = "win32yank-wsl",
copy = {
'+' = "win32yank.exe -i --crlf",
'*' = "win32yank.exe -i --crlf"
},
paste = {
'+' = "win32yank.exe -o --crlf",
'+' = "win32yank.exe -o --crlf"
},
cache_enable = 0,
}
endif

call SpaceVim#begin()

call SpaceVim#custom#load()

call SpaceVim#default#keyBindings()

call SpaceVim#end()

call SpaceVim#logger#info('finished loading SpaceVim!')
" vim:set et sw=2 cc=80:

neovim 官方配置wsl剪切板 文档在这 我试了没有用 你不信自己再试试
https://neovim.io/doc/user/provider.html#clipboard-wsl

希望这篇文章对你有帮助