Chris Bailey

Vim and the System Clipboard on WSL

This will be a short one concerning some strange behaviour that frustrated me for some time when I decided to format and re-setup my development laptop for work.

I set up my development environment just how I usually do, as per blog post detailing setting up WSL, but I quickly noticed that whilst everything was working perfectly fine, copying and pasting seemed to be broken on Vim.

Copying and pasting from Vim

Usually when you yank or delete text objects in Vim, they get stored in a Vim buffer. Vim has a very nice and usually transparent configuration option to tell Vim to act like other applications and copy to the System Clipboard instead. You can tell Vim to do this by adding the following line in your .vimrc or your init.vim:

set clipboard=unnamedplus
if has('win32')
    set clipboard=unnamed

This works out of the box on any Linux distribution I have ever installed; and generally you don't need to even think twice about it. It just configures Vim to act how I expect copy/pasting stuff to work.

This isn't so easy in WSL world however, because WSL is essentially just Linux, and the System Clipboard runs on Windows. I usually got around this problem by installing an X-Server on Windows (such as X410 or VcrXsrv—I change between these two often). These X-Servers allow applications that are running in Linux (WSL) to not only draw GUIs as native windows on Windows, but also allows for clipboard sharing.

But for some reason this didn't work... I searched Google for some answers assuming this was some regression in WSL, X410 or VcrXsrv but nothing seemed to work. I had toyed around with more in depth hacks to get this kind of behaviour working when I used to use Cygwin as a UNIX-like environment on Windows but that wasn't a route I wanted to revisit.

The most up to date information I could find was this related GitHub issue which seems to suggest it worked as intended, which it didn't, so I began debugging the issue myself.

The Fix

I tried ignoring my X-Server's copy and paste functionality by trying writing a naive xclip implementation that I'd get vim to call, utilizing the following fun facts:

  1. Because of Windows interoperability from within WSL2, you can read from the Windows clipboard via:
    powershell.exe -Command Get-Clipboard
  2. You can put things into the Windows clipboard with:
    clip.exe < "hello, world!"

However, the time to start up the powershell.exe interpreter is very noticeable (~1.5s) and you always get newlines pasted no matter what you try, when vim is set up to paste from a Windows application because of carriage returns; though whilst this approach ended up not being the solution, it eventually lead to the solution.

Since my own attempt at working around the situation wasn't great, I started trying to debug what was actually wrong. My first step was wanting to see if something like xsel or xclip would be able to write to the Windows clipboard via X-Server to determine if this was a WSL/X-Server issues versus a Vim one. It seems like the Ubuntu distro on the Microsoft Store didn't come with xclip installed it with sudo apt-get install xclip and tried it.

To my surprise, I did: echo 'hello world!' | xclip -selection clipboard and it worked!

Vim's copy and paste started working too... so it turns out that Vim will silently not use the system clipboard if no clipboard manager (i.e. xclip or xsel) is installed... and Vim will fall back to using buffers.

I definitely don't remember needing to do this in the past, but I might be mistaken. Hopefully this helps some people who are falling into the same trap 😅

Return to Posts →