5
votes

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?

3
If you don't want vim to handle your mouse so you can still highlight and copy as you would anything else from the terminal, you should be able to :set mouse=Christian Gibbons
Can you use clip.exe to access the clipboard from WSL? If so, select the lines visually them run :w !clip.exe. (Source for the clip.exe suggestion: raymondcamden.com/2017/10/19/…)filbranden
I'm using Neovim and went with the instructions on their FAQ at github.com/neovim/neovim/wiki/…. Otherwise, I think How to “copy to clipboard” in vim of Bash on Windows? can help you out. There's a few solutions that don't require installing an X server on Windows.Andrey Kaipov

3 Answers

4
votes

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
1
votes

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 neovim set clipboard=unnamedplus or vim set 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.

if uname -r | grep 'microsoft' > /dev/null 
  set -l LOCAL_IP (cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
  set -xg DISPLAY $LOCAL_IP:0
end

0
votes

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.

  1. On the Linux side, install a clipboard-enabled build of Vim. The vim-gtk package is fine.

  2. 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).

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

  4. In Vim, on the Linux side, use either "+ or "* as you would if you where on a genuine Linux box.