a little place I like to call home.

Vim + WSL + SSH + Windows Clipboard

May. 27, 2020

This post is about getting Vim + WSL + SSH working nicely for copy and pasting text from Vim. Tmux support will likely be coming in the near future since you aren't always running commands within Vim. For now you can use Shift+selection from a Tmux window to copy text quickly.

Back to setting up Vim to talk to clip.exe when you yank.

This is the clipboard daemon that will accept commands on localhost and copy them to the windows clipboard.

while IFS= read -r LINE || [[ -n "$LINE" ]]; do echo "$LINE" | clip.exe; done < <(nc -k -l localhost -p 19988)

Run this from a new WSL window. You'll always need to run this if you want clipboard support to work. If you notice that clipboard support stopped working or doesn't work make sure this is running.

Now you need to setup a RemoteForward tunnel within SSH. This could be adapted to work on localhost, but my target was a remote SSH server.

Within your WSL ~/.ssh/config

Host dev
	User silas
	RemoteForward 19988 localhost:19988

Which will forward the port defined in your WSL instance to the box that you are ssh'ing into.

In your VIMRC file:

autocmd TextYankPost * call system('echo '.shellescape(join(v:event.regcontents, "\<CR>")).' | nc localhost 19988 -C -q 1')

Now when you yank in Vim it will call the autocmd command which will pass the contents of the register to localhost 19988.



comments powered by Disqus