Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to copy text from vim to system clipboard? #892

Closed
fcbruce opened this issue Aug 14, 2016 · 19 comments
Closed

How to copy text from vim to system clipboard? #892

fcbruce opened this issue Aug 14, 2016 · 19 comments

Comments

@fcbruce
Copy link

fcbruce commented Aug 14, 2016

I want to copy the text from vim to my windows apps(such as browser), I try to use "+y but it does not work. If there are some command can solve my problem? I know I can quit vim then cat file.txt and use mouse to do it, but I want to copy the text without mouse.

@fpqc
Copy link

fpqc commented Aug 14, 2016

@fcbruce not in an easy and obvious way. You need to set up a pair of programs on both sides of the divide, one attached to the Windows clipboard, and one attached to the vim clipboard. If anyone has dealt with this problem, it might be something like PuTTY or cygwin/cygterm, which might have something built in, since they bridge this kind of divide inherently.

@Blaisorblade
Copy link

@fpqc Note that "+y connects to the X11 clipboard, not to the Vim one, making this easier to handle. Indeed this already works in MSYS2, and without an X-server — install Git for Windows, start Git Bash, use vim inside. (Not sure how much magic is required for that though).

@fpqc
Copy link

fpqc commented Aug 14, 2016

there ya go, fcbruce! You have your answer, use an X11 terminal.

@fcbruce
Copy link
Author

fcbruce commented Aug 15, 2016

@fpqc @Blaisorblade Yes, it works. I installed Xming X Server for Windows and input the command export DISPLAY=:0 in the bash of linux subsystem. Thank you very much! ^_^

@fcbruce fcbruce closed this as completed Aug 15, 2016
@robbiev
Copy link

robbiev commented Jan 28, 2017

For those who want to go the X server route, let me leave my notes here.

  1. Install VcXsrv (I found that Xming is outdated on sourceforge and the new version is donationware)
  2. If it starts after installing, stop it
  3. Start it using XLaunch (search in the start menu), go with all the defaults (ensure the clipboard options are checked)
  4. At the end, save the configuration to a file (use that to start it from now on)
  5. Put export DISPLAY=localhost:0.0 in your .bashrc in bash for Windows (and run the command in any open bash windows). The reason I explicitly say localhost is that this makes SSH X forwarding work, see below.
  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. If you don't have clipboard support, install the vim-gtk package (apt-get install vim-gtk).
  7. It should now work

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.

@ggada
Copy link

ggada commented Apr 6, 2017

@robbiev Thank you for this! You are a lifesaver :)

@JonahKlimack
Copy link

JonahKlimack commented Jul 26, 2017

steps 1-5 worked for sublime (xming, not windows version of sublime) as well which may not be obvious to everyone

@lemongrassnginger
Copy link

lemongrassnginger commented Oct 25, 2017

for those using the VcXsrv approach by @robbiev, I was trying to find a way to start VcXsrv without having to start it manually by the XLaunch GUI.

Adding a line like this to ~/.bashrc works for me:

# start Xserver for shared clipboard
"/mnt/c/Program Files/VcXsrv/xlaunch.exe" -run config.xlaunch

It requires a config file, which can be generated from the last screen of the XLaunch GUI, just save it in the same folder as the xlaunch executable

@Ossur
Copy link

Ossur commented Nov 9, 2017

@lemongrassnginger I think this will start a new server process for every shell. For me it worked to copy config.xlaunch to %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup so that Windows launches an instance at startup.

@lemongrassnginger
Copy link

So it does! Thanks for pointing that out. Will move to my Windows startup folder

@kennedymj97
Copy link

For people sitll looking for a solution I found a way that works without having to use X server. Thanks to bravekarma on reddit.

Simply paste the following into your .vimrc or init.vim:

" 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

@SiddharthPant
Copy link

SiddharthPant commented Jun 4, 2020

Ok I found a better way with both copy and paste working. I tested for neovim but with some modification will sort of work for vim also.
You can see the original solution over this stackoverflow answer.
Basically involves downloading win32yank.exe. It gets used by default with neovim, for vim you will have to edit your .vimrc. For details on vim modifications please see the original answer over stackoverflow as he gives out better details than what I can. Proceed below when those are done.
In init.vim or .vimrc put set clipboard=unnamedplus.
Execute the following commands:

curl -sLo /tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.0.4/win32yank-x64.zip
unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
chmod +x /tmp/win32yank.exe
sudo mv /tmp/win32yank.exe /usr/local/bin/

@3n16m4
Copy link

3n16m4 commented Jul 12, 2020

For people sitll looking for a solution I found a way that works without having to use X server. Thanks to bravekarma on reddit.

Simply paste the following into your .vimrc or init.vim:

" 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

thank you. this is by far the best solution.

@shaahiiin
Copy link

Ok I found a better way with both copy and paste working. I tested for neovim but with some modification will sort of work for vim also.
You can see the original solution over this stackoverflow answer.
Basically involves downloading win32yank.exe. It gets used by default with neovim, for vim you will have to edit your .vimrc. For details on vim modifications please see the original answer over stackoverflow as he gives out better details than what I can. Proceed below when those are done.
In init.vim or .vimrc put set clipboard=unnamedplus.
Execute the following commands:

curl -sLo /tmp/win32yank.zip https://github.com/equalsraf/win32yank/releases/download/v0.0.4/win32yank-x64.zip
unzip -p /tmp/win32yank.zip win32yank.exe > /tmp/win32yank.exe
chmod +x /tmp/win32yank.exe
sudo mv /tmp/win32yank.exe ~/bin

The answer is based on this Neovim wiki page.
You have to ensure that you're placing win32yank.exe in your path (the command assumes that ~/bin is in your path)

douglarek added a commit to douglarek/.space-vim.d that referenced this issue Feb 7, 2021
@nic07in
Copy link

nic07in commented Jan 3, 2022

If the solution with VcXsrv described by @robbiev stopped working when upgrading from WSL1 to WSL2, do the following:
- In XLaunch settings check "Disable access control"
- Put in .bashrc
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=0
Found here.

@alexcoca
Copy link

For people sitll looking for a solution I found a way that works without having to use X server. Thanks to bravekarma on reddit.

Simply paste the following into your .vimrc or init.vim:

" 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

This is a brilliant answer, I think this works on my machine. However, if I ssh into a machine and try to yank those files to the clipboard, it no longer works? How to overcome this issue?

@WorksAtAUni
Copy link

These are nice workarounds, but considering who makes WSL. it is a wonder that "* and "+ do not work at all out of the box, that there is no underlying mechanism between it and the system clipboard. if you type :registers they don't even show. "*dd and "+dd function as though successful, but aren't. If you do "*10yy and "+10yy you get the 10 lines yanked message, but it will yank into unnamed register and the yank register. "a10yy yanks into the "a register properly.

I think @fcbruce and @alexcoca are still more correct than not that is should be an enhancement request or a bug report of some kind

@daviewales
Copy link

For those who want to go the X server route, let me leave my notes here.
...

If you're on Windows 11, WSL has an X server configured out of the box.
The steps required for copy-paste are reduced simply to:

  1. sudo apt install vim-gtk

This switches the installed options from -clipboard to +clipboard, and "+y will work immediately.
"*y still doesn't work, I'm not sure why.

@soleera
Copy link

soleera commented Jul 25, 2023

For people sitll looking for a solution I found a way that works without having to use X server. Thanks to bravekarma on reddit.

Simply paste the following into your .vimrc or init.vim:

" 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

Here's a translation into Lua for any Neovim users that come across this issue:

-- WSL yank support
do local clip, opts = '/mnt/c/Windows/System32/clip.exe', {}
  function opts.callback()
    if vim.v.event.operator ~= 'y' then return end
    vim.fn.system(clip, vim.fn.getreg(0))
  end
  if vim.fn.executable(clip) then
    opts.group = vim.api.nvim_create_augroup("WSLYank", {clear = true})
    vim.api.nvim_create_autocmd("TextYankPost", opts)
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests