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.
- Windows 10 Home
- Ubuntu 18.04 on Windows Subsystem for Linux 2 (WSL2)
- Build Vim with the clipboard option enabled
- Set up VcXsrv Windows X Server
- Connect VcXsrv from Ubuntu on WSL2
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
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
- Clipboard (default)
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.
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!
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)
inipconfig
). 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 includeipconfig.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.