Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set up Vim on Ubuntu on Windows Subsystem for Linux 2 (WSL2) to share clipboard

This explains how to set up Vim on Ubuntu 18.04 on Windows Subsystem for Linux 2 (WSL2) in order to share the clipboard between Windows and Ubuntu.

Environments

  • Windows 10 Home
  • Ubuntu 18.04 on Windows Subsystem for Linux 2 (WSL2)

Steps

  1. Build Vim with the clipboard option enabled
  2. Set up VcXsrv Windows X Server
  3. Connect VcXsrv from Ubuntu on WSL2

Build Vim with the clipboard option enabled

Install necessary dependencies to build Vim with the clipboard option enabled:

sudo apt install ncurses-dev libncurses5-dev libgnome2-dev libgnomeui-dev libgtk2.0-dev libatk1.0-dev libbonoboui2-dev libcairo2-dev libx11-dev libxpm-dev libxt-dev python-dev python3-dev ruby-dev lua5.1 lua5.1-dev libperl-dev

N.B. Not all of them are required to build Vim with +clipboard, so skip installing unnecessary libraries if you want

Build Vim following the official guide:

cd /usr/local/src
sudo git clone https://github.com/vim/vim.git
cd vim/src
sudo make distclean  # if you build Vim before
sudo make
sudo make install

2. Set up VcXsrv Windows X Server

Download and install VcXsrv Windows X Server, then run XLaunch with the following options:

  • Multiple windows (default)
  • Start no client (default)
  • Extra settings
    • Clipboard (default)
      • Primary Selection (default)
    • Native opengl (default)
    • Disable access control

Basically, you just need to tick all of the extra options. Other than that, every setting is the default. Click the [Save configuration] button and save the configuration in C:\Users\<USER NAME>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup in order to start VcXsrv when Windows starts.

3. Connect VxXsrv from Ubuntu on WSL2

Log in to Ubuntu on WSL2 and set the DISPLAY environment variable:

LOCAL_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
export DISPLAY=$LOCAL_IP:0

That's it! Enjoy your Vim life on Windows!

References

@drazik

This comment has been minimized.

Copy link

drazik commented Feb 23, 2020

It worked well with neovim. I just skipped the first step and installed neovim with instructions from https://github.com/neovim/neovim/wiki/Installing-Neovim#linux, then followed steps 2 and 3 and 🎉

@Zantier

This comment has been minimized.

Copy link

Zantier commented Mar 13, 2020

Instead of building vim yourself, you can simply install the package vim-gtk. After installing, if you run vim --version | grep clipboard, you should see +clipboard.

I also had to go into Windows Firewall, find the inbound rules named VcXsrv windows xserver, and change them from "Block the connection" to "Allow the connection".

The rest worked great, thank you!

@lefth

This comment has been minimized.

Copy link

lefth commented May 23, 2020

Using the IP of the nameserver in recolv.conf didn't work in my testing, because the given IP address corresponded to the virtual ethernet address of WSL (titled Ethernet adapter vEthernet (WSL) in ipconfig). According to the documentation, this should be the host IP address, but it's not. (I'm testing in a freshly installed WSL2, Ubuntu.)

This is the command I had to use to get the windows IP address:
LOCAL_IP=$(ipconfig.exe | awk 'BEGIN { RS="\r\n" } /^[A-Z]/ { isWslSection=/WSL/; }; { if (!isWslSection && /IPv4 Address/) { printf $NF; exit; }}')
The WSL $PATH must be set up to include ipconfig.exe, which for me means it needs to include /mnt/c/Windows/system32.

If you use VMWare or VirtualBox, the WSL regular expression needs to be modified so as to also ignore sections that show the IP addresses of those virtual hosts.

@artkpv

This comment has been minimized.

Copy link

artkpv commented Aug 1, 2020

IMO, That's overhead. Using Ctrl+Shift+C, Ctrl+Shift+V from Windows Terminal does the thing. KISS

@drazik

This comment has been minimized.

Copy link

drazik commented Aug 5, 2020

I don't understand your point @artkpv 🤔 Using Ctrl+Shift+C will also copy line number and other garbage. The thing here is to be able to yank from vim to the system clipboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.