Windows Subsystem Linux - Make VIM use the clipboard?

38,879

Solution 1

(Edit: Oct 2020) For 2-way clipboard on neovim, I have been using win32yank for several months with no issues. Put win32yank.exe somewhere in your path on Linux (anywhere should be fine), and add the following to your init.vim.

set clipboard+=unnamedplus
let g:clipboard = {
          \   'name': 'win32yank-wsl',
          \   'copy': {
          \      '+': 'win32yank.exe -i --crlf',
          \      '*': 'win32yank.exe -i --crlf',
          \    },
          \   'paste': {
          \      '+': 'win32yank.exe -o --lf',
          \      '*': 'win32yank.exe -o --lf',
          \   },
          \   'cache_enabled': 0,
          \ }

(Original answer) If you just want to yank from VIM to Windows, for WSL2 and Ubuntu 20.04, this answer on Reddit worked perfectly for me with standard VIM and standard WSL2 Ubuntu.

Put the following in your .vimrc (at the bottom, for example):

" WSL yank support
let s:clip = '/mnt/c/Windows/System32/clip.exe'  " change this path according to your mount point
if executable(s:clip)
    augroup WSLYank
        autocmd!
        autocmd TextYankPost * if v:event.operator ==# 'y' | call system(s:clip, @0) | endif
    augroup END
endif

Solution 2

This solution worked for me, thanks to github user robbiev.

For completion this is an outtake from his answer:

  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.

  8. Now you can access the Windows system clipboard via "*p and "*y, or set it to default by putting set clipboard=unnamed in your .vimrc file.

As robbiev mentions you should now also be able to use the Windows clipboard from remote machines using SSH X forwarding.

Solution 3

You can right click the terminal then in the options box check CTRL + SHIFT + C/V for copy paste operations through the terminal.

WSL Ubuntu Terminal

Solution 4

A partial solution through ConEmu

Not sure why nobody mentioned ConEmu. It can at minimum handle one-half of the clipboard issue:

  • To paste Windows clipboard stuff into WSL, normal Ctrl + V will get things right.

    • One perk is that: if one is to copy an absolute path in Windows, when pasting in ConEmu, c:/users/name will be transcribed into /mnt/c/users/name. This is runnable through WSL natively.
  • To copy from WSL, for now, I would still have to use my house cursor. It works more than 90% of the times.

Per my use case, I don't tend to copy from WSL a lot; and have been enjoying the pasting-help by ConEmu a lot.

Solution 5

If you have enabled the QuickEdit Mode, you can just select the text with your mouse and right click to copy it into the clipboard.

For enabling the QuickEdit Mode, just right-click on the console windows on top and select Properties (and/or Default), then tick QuickEdit Mode.

EDIT: Out-of-the-box, it is not possible to copy from VIM into Windows clipboard currently. However, on a GitHub-Issue within the WSL repository, some guys seem to have found a way to do that using Xming or VcXsrv respectively: https://github.com/Microsoft/WSL/issues/892#issuecomment-275873108.

EDIT2: Another try using .vimbuffer: https://stackoverflow.com/questions/44480829/how-to-copy-to-clipboard-in-vim-of-bash-on-windows

Share:
38,879

Related videos on Youtube

dman
Author by

dman

Updated on September 18, 2022

Comments

  • dman
    dman over 1 year

    To get this off the table now and avoid any confusion ... This is for Linux running in Windows 10(ubunutu), also known as WSL. Its not the same as cygwin and windows or stand alone linux. It is it's own beast.

    So please keep the above in mind before I am referenced with all the articles I have already read or comment that this has been asked before.

    Does anyone know how to make the copy and paste work off of the windows clipboard OR the WSL Ubuntu Linux emulation? I am using set clipboard=unnamedplus. It's not working, not matter what combinations I use like yy, "+yy, etc.

    Yes, it's vim-gtk with +xterm_clipboard support.

    • AFH
      AFH about 6 years
      If you're running Win10 pre-1709, then WSL is beta software obtained through the developer channel. Since then, the official release is Ubuntu (or OpenSUSE) available through Linux on Windows from MS Store. Despite your clarification, it's not clear which you're running. That said, I've found that the standard cmd terminal clip-board handling seems to work on both.
    • dman
      dman about 6 years
      No, this is most recent...non beta..from the store. I just installed it 3 days ago. I can't get vim to use the clipboard (rather it be windows or linux ...not sure which would be used in the unique WSL case).
    • AFH
      AFH about 6 years
      It works for me in 1709: I have configured the bash terminal for Quick Edit mode and I use click and drag to select, right-click to copy selection and again to paste.
    • dman
      dman about 6 years
      @AFH just to confirm, you are able to yank contents in vim using the vim yank command(ie: yy), have the yanked contents go to the clipboard, and paste from the clipboard using vim's paste (ie pp) in a separate vim session?
    • AFH
      AFH about 6 years
      That seems to work: I typed 5yy to copy 5 lines, restarted vim, and p pasted the 5 lines.
    • dman
      dman about 6 years
      @AFH I have that behavior also. But it's a different behavior if you have two separate vim sessions at the same time and you try to go back and forth with the pasting.
    • AFH
      AFH about 6 years
      Do you mean in separate bash windows? I'm not surprised if this is the case, as the two sessions would likely be independent of each other. But using Windows copy and paste will work between them.
    • dman
      dman about 6 years
      @AFH yep... I used to be able to do this with two separate cygwin sessions using the windows clipboard registers. Any ideas on how to do it in WSL?
    • AFH
      AFH about 6 years
      Same way, as per my second comment. The paste can be done in any window, terminal-based or otherwise.
    • Christian Brabandt
      Christian Brabandt about 6 years
      perhaps this does it? vi.stackexchange.com/q/15182/71
    • dhaupin
      dhaupin about 6 years
      To take this to a more general level, I think the gist of this would be "paste into vim from windows clipboard", not the reverse. Like if I were to copy a snippet for a config in windows browser, then try to paste that snippet into WSL term with vim open, nothing seems to work.
  • dman
    dman about 6 years
    no, the whole idea is to use the vim commands yy and not have to use the mouse.
  • dman
    dman over 5 years
    How do you get Xlaunch to start VcXsrv? It defaults to Xming for me
  • Aerows
    Aerows over 5 years
    The joy when you (me) face with same problem 4 months later, google it and your (mine) past self helps you fix it!
  • Charles Williams
    Charles Williams about 4 years
    I did find that you have to use a different terminal besides the default WSL terminal that comes from installing the your distro of choice. Thanks for leading me in the right direction. I use Terminus in case anyone was wondering. Works great as a WSL terminal.
  • llinfeng
    llinfeng about 4 years
    I now use wsltty as the emulator, which even supports mouse-click events. It is my #1 recommendation for now, after 2+ years with WSL :)
  • PabTorre
    PabTorre over 3 years
    Also works fine on Debian with WSL1
  • foxiris
    foxiris over 3 years
    The defect is If file lines exceed screen height, you can't select all lines in the vim.
  • walnut_salami
    walnut_salami almost 3 years
    I didn't really need to do the let g:clipboard thing. neovim-0.5.0+ubuntu2+git202105110234-133351cbf-d569569c9 supported it out of the box. :checkhealth returns "OK: Clipbard tool found: win32yank". It's also worth mentioning win32yank is something you install on the Windows side, not the WSL side (choco install win32yank)
  • AntonOfTheWoods
    AntonOfTheWoods almost 3 years
    @3nuc, you don't need to "install" win32yank. It is a self-contained binary. It should be executable from Linux and doesn't need to be available for Windows at all. I guess your Windows choco bin path is added to the WSL path, hence why doing it your way would work. It might even be more practical but it is not "something you install on the Windows side" (at least not something that needs to be anyway).
  • roachsinai
    roachsinai almost 3 years
    Thanks for your post! Is there a way to let yG copy thing to vim clipboard, and <leader>yG copy things to Windows clipboard?
  • xaa
    xaa over 2 years
    thanks for the snippet. it's working but not if Vim open in tmux. Any suggestion ? Thank you
  • AntonOfTheWoods
    AntonOfTheWoods over 2 years
    @xaa, this has been working for me with tmux-yank since the beginning, try that.