Skip to content

Instantly share code, notes, and snippets.

Last active June 2, 2021 18:54
What would you like to do?

Vim System Clipboard Support

WSL vim

  1. Install VcXsrv (if it starts after installing, stop it).

  2. Start it using the newly installed program XLaunch (search in the start menu).

  3. Go with all the defaults options, and ensure the clipboard options are checked.

  4. At the end, save the configuration to a file, config.xlaunch (use that to start it from now on).

  5. Put export DISPLAY=localhost:0.0 in your .bashrc in bash for Windows and run source ~/.bashrc in any open terminal.

  6. Ensure vim is installed using clipboard support. vim --version | grep clipboard should say +clipboard, not -clipboard. Also if you run the ex command :echo has('clipboard') in vim and it says 0 it does not have clipboard support compiled in.

  7. If you don’t have clipboard support, install a vim package compiled with clipboard support, e.g. apt-get install vim-gtk or sudo dnf install vim-X11

As a bonus you should now be able to copy from and to your Windows clipboard from a remote machine by using SSH X forwarding (ssh -X …​ ). You can use xclip on the remote machine or if you use vim there you will again need to make sure the clipboard option is compiled into vim (e.g. install vim-gtk). You can probably also configure PuTTY to use your local X server in case you prefer using that for remote connections.

System Clipboard Vim within TMUX within SSH session

didn’t try it myself, yet.

SSH X forwarding

  • On your remote system, install a clipboard-aware Vim (and the X dependencies needed for clipboard support):

    $ sudo apt-get install vim-gtk
  • On your local system, start your ssh session with X11 forwarding enabled:

    $ ssh -X user@hostname

See $ man ssh for the security implications of X11 forwarding.

Terminal with OSC support

Clipboard integration feature (PASTE64/OSC52) is helpful if your terminal emulator supports it. For example, iTerm2 supports it (I am not sure about Ubuntu).

Add this function to your "remote" .vimrc. yank something and run :OscCopy. It works even it is inside tmux session.

function! OscCopy()
  let encodedText=@"
  let encodedText=substitute(encodedText, '\', '\\\\', "g")
  let encodedText=substitute(encodedText, "'", "'\\\\''", "g")
  let executeCmd="echo -n '".encodedText."' | base64 | tr -d '\\n'"
  let encodedText=system(executeCmd)
  if $TMUX != ""
    let executeCmd='echo -en "\x1bPtmux;\x1b\x1b]52;;'.encodedText.'\x1b\x1b\\\\\x1b\\" > /dev/tty'
    let executeCmd='echo -en "\x1b]52;;'.encodedText.'\x1b\\" > /dev/tty'
  call system(executeCmd)
command! OscCopy :call OscCopy()
There is a plugin:

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