Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to copy/paste from Vim in WSL

I am trying to use Vim to locate and copy/paste some code I need to analyze and take notes on. I am using Debian, in a Windows WSL environment. That's what makes this tricky.

The regular "yank and put to global register" commands "+y and "*y commands didn't work.

On the other hand, the brute force approach where I just use the mouse to scrape the terminal text won't work either. Strangely, WSL terminal has mouse support, and Vim can track its movements, select in visual mode, etc. So Vim intercepts the selection command, and then there is nothing selected for ctrl-shift-c to copy into the Windows clipboard.

I know the WSL terminal supports copy and paste, and I can successfully do it if I cat my file to the screen, and copy and paste that using ctrl-shift-c and ctrl-v. But then I lose out on ease of navigation.

What's the best way to copy text out of Vim inside a WSL terminal and into the windows clipboard?

like image 563
nomen Avatar asked May 01 '20 20:05

nomen


People also ask

How do I enable copy paste in wsl2?

The default behavior for new users in Windows Terminal is to enable Ctrl C / Ctrl V for Copy/Paste.

How copy paste Windows Vim Ubuntu?

To go back to normal mode from any other mode, just press the Esc key. Vim has its own terminology for copying, cutting, and pasting. Copy is called yank ( y ), cut is called delete ( d ), and paste is called put ( p ).

How to use WSL Vim copy and paste on Mac?

One of them is the WSL Vim copy and paste. In Windows, there is no the commonly used Command Key as a Mac. The previously Command+C/Command+V works as Ctrl+C/Ctrl+V in Windows. However, Ctrl keys in the terminal especially the WSL (Ubuntu Linux) means something very different.

Why can't I copy and paste from WSL to clipboard?

Strangely, WSL terminal has mouse support, and Vim can track its movements, select in visual mode, etc. So Vim intercepts the selection command, and then there is nothing selected for ctrl-shift-c to copy into the Windows clipboard.

How to copy and paste in Vim?

Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor. In this guide, we have shown you how to copy, cut, and paste in Vim. If you are new to Vim, visit the Open Vim site where you can practice Vim with an interactive tutorial.

Can I copy and paste between a WSL terminal and Windows?

By the end of this video you’ll have your clipboard working across all terminal applications, and even be able to copy and paste between a WSL terminal and Windows. Also, if you’re not using WSL most of the video applies to MacOS and native Linux too.


Video Answer


2 Answers

Answer is, do a vim visual selection then do the command:

:'<,'>w !clip.exe

This pipes the current selection out to the shell command clip.exe, which utilizes WSL's ability to execute Windows executables (even with a pipeline). Text piped to clip.exe goes to the Windows clipboard.

Also, this command saves the whole file to the clipboard (not the requirement):

 :w !clip.exe
like image 76
fencemaker Avatar answered Oct 16 '22 19:10

fencemaker


Like romainl mentioned, clipboard is at X level. So the most important step is you need to have an X-server running on Windows, and you need to set DISPLAY variable on Linux to point to X-server. Then in neovim set clipboard=unnamedplus or vim set clipboard=unnamed to link to the system clipboard.

Follow this nice gist should make things work.

For me I use fish shell, the wsl specific logic becomes in your config.fish.

if uname -r | grep 'microsoft' > /dev/null 
  set -l LOCAL_IP (cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
  set -xg DISPLAY $LOCAL_IP:0
end

like image 1
LeOn - Han Li Avatar answered Oct 16 '22 18:10

LeOn - Han Li