WSL环境下VIM的剪切板无法与Windows共享
这是一个由来已久的问题,今天看到StackOverflow上一个堪称惊艳的解决方案,即便是VIM版本不支持clipboard特性(vim --version | grep clipboard
查看是否支持),依然可以将VIM中的文本共享到Windows的剪贴板上。
使用clip.exe完美解决
Answer is, do a vim visual selection then do the command:
:'<,'>w !clip.exe
This pipes the current selection out to the shell command clip.exe, which utilizes WSL’s ability to execute Windows executables (even with a pipeline). Text piped to clip.exe goes to the Windows clipboard.
Also, this command saves the whole file to the clipboard (not the requirement):
:w !clip.exe
解释一下,clip.exe
是windows平台上system32目录下的一个可执行程序。我们知道WSL可以使得Linux子系统访问到Windows的path路径(这也是WSL,相比传统虚拟机方式的优势),因此可以直接在VIM的命令模式下将选区内容(’<,’>w)以管道的方式传递给clip.exe程序,最终写入到windows的剪切板上。而且这种方式,可以直接将文件整个内容复制到剪切板,简直强无敌!