wsl2 下输出重定向至 clip.exe 出现中文乱码问题解决方案

发布时间 2023-08-18 21:33:13作者: AsindE

背景

win10 系统在 wls2 下安装 neovim 后希望与 windows 剪切板通信。

按教程添加如下配置。

-- 系统剪切板
if vim.fn.has('wsl') then
	vim.g.clipboard = {
		name = 'WslClipboard',
		copy = {
			['+'] = 'clip.exe',
			['*'] = 'clip.exe'
		},
		paste = {
			['+'] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
			['*'] = 'powershell.exe -c [Console]::Out.Write($(Get-Clipboard -Raw).tostring().replace("`r", ""))',
		},
		cache_enabled = 0,
	}
end

配置完成后可正常复制粘贴,但当内容出现中文时,windows 剪切板出现乱码。

解决方案

将系统编码设置为 UTF-8 格式,将 Bata 版:使用 Unicode UTF-8 提供全球语言支持(U) 选项开启即可。