Skip to content

Instantly share code, notes, and snippets.

@necojackarc
Last active December 28, 2023 02:45
Show Gist options
  • Star 38 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save necojackarc/02c3c81e1525bb5dc3561f378e921541 to your computer and use it in GitHub Desktop.
Save necojackarc/02c3c81e1525bb5dc3561f378e921541 to your computer and use it in GitHub Desktop.
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

@artkpv
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
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.

@mbomb007
Copy link

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

That doesn't work when in Vim.

@Anogoya-Dagaate
Copy link

For what its worth the VcXsrv route generated a couple issues on the latest WSL2; ended up brute-forcing it with
/mnt/c/Windows/System32/WindowsPowershell/v1.0/powershell.exe -command "Get-Clipboard" >> ~/target I wouldn't want to make a habit of it but it got me where I needed to be . . . . for now!
FYI It wrecked tmux rendering momentarily but seemed to recover after a reset

@shubhamdhama
Copy link

Followed the steps in the Gist and the following additional steps to make it work.
I faced this https://superuser.com/q/1476086/989837 issue, so had to follow both of these answers,

  1. https://superuser.com/a/1476160/989837 Add -ac in the "Additional parameters for VcXsrv" extra option.
  2. https://superuser.com/a/1620003/989837 Close any running VcXsrv, Delete all firewall rules for VcXsrv, and relaunch.
  3. Maybe restart isn't required but I did that anyway and it worked.

Note: Maybe you don't need to delete the firewall rules and just do this as suggested by Zantier.

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!

Thank you @necojackarc!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment