I am trying to use Vim to locate and copy/paste some code I need to analyze and take notes on. I am using Debian, in a Windows WSL environment. That’s what makes this tricky.
The regular “yank and put to global register” commands "+y
and "*y
commands didn’t work.
On the other hand, the brute force approach where I just use the mouse to scrape the terminal text won’t work either. Strangely, WSL terminal has mouse support, and Vim can track its movements, select in visual mode, etc. So Vim intercepts the selection command, and then there is nothing selected for ctrl-shift-c to copy into the Windows clipboard.
I know the WSL terminal supports copy and paste, and I can successfully do it if I cat
my file to the screen, and copy and paste that using ctrl-shift-c and ctrl-v. But then I lose out on ease of navigation.
What’s the best way to copy text out of Vim inside a WSL terminal and into the windows clipboard?
4
Answers
On Linux, Vim’s clipboard support is intimately tied to X. If you want the same level of integration between WSL and the rest of Windows as you are used to in a proper Linux Box you will have to install a Windows X Server.
On the Linux side, install a clipboard-enabled build of Vim. The
vim-gtk
package is fine.On the Windows side, install an X Server like VcXsrv (there are many alternatives, you are on your own to find the one that best suit your needs).
You generally have to edit a couple of configuration files on the Linux side for your X clients to use the right X Server. What to do exactly will depend on the X Server you choose.
In Vim, on the Linux side, use either
"+
or"*
as you would if you where on a genuine Linux box.Like romainl mentioned, clipboard is at X level. So the most important step is you need to have an X-server running on
Windows
, and you need to set DISPLAY variable on Linux to point to X-server. Then in neovimset clipboard=unnamedplus
or vimset clipboard=unnamed
to link to the system clipboard.Follow this nice gist should make things work.
For me I use fish shell, the wsl specific logic becomes in your
config.fish
.Answer is, do a vim visual selection then do the command:
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 toclip.exe
goes to the Windows clipboard.Also, this command saves the whole file to the clipboard (not the requirement):
I have "WSL Debian" installed and I installed a
neovim
package inside. PAckage installs anvim
text editor without a clipboard support. I can verify this like this:Note that under
Optional features included
there is no+clipboard
entry and this means that mynvim
doesn’t support clipboard out of the box.Then I open the
nvim
editor and execute a command:CheckHealth
to get this feedback:This tells me (a) that clipboard is not working currently and (b) to use a command
:help clipboard
inside thenvim
to get more info. So I execute this command and I can read this:This tells me that even though clipboard support was not compiled in, it is still possible to implicitly enable it if we only install e.g.
xsel
. So I do it:This also tells me to put the below line in my
~/.config/nvim/init.vim
:It looks like this should already be solved, but at this point things will not yet work. Why is that? This is because
xsel
(as it’s name implies) is a graphical application that needs X server in order to run!So we install X server for Windows! One option is to simply install "VcXSrv"(link). This will create a "Xlaunch" launcher in the Windows start menu. We run this launcher and just click next until the "Extra settings" window. Here we make sure to check all the boxes like shown below and click "Next".
Now we will store our "Xlaunch" configuration by pressing "Save Configuration" and make sure to store our configuration as:
This will make sure that X server will start with the same configuration whenever Windows starts! Note that X server has to be running before starting the "WSL Debian".
Now we click "Finish" and for this session X server will run.
Now inside the "WSL Debian" we only have to export a
DISPLAY
enviromental variable which is where X server applications e.gxset
will search for a working X server session. Our job is to point them to our Windows machine! So we can exportDISPLAY
like this:If you want you can add these two lines in the
~/.bashrc
file on "WSL Debian" so that they will always be exported when you open an interactive terminal.Now everything should work.