Skip to content

Instantly share code, notes, and snippets.

@pprzetacznik
Last active May 3, 2022 20:39
Show Gist options
  • Save pprzetacznik/d531d9d50f3c88ea170b003668e7e2b0 to your computer and use it in GitHub Desktop.
Save pprzetacznik/d531d9d50f3c88ea170b003668e7e2b0 to your computer and use it in GitHub Desktop.
Clipboard with WSL2 and remote hosts

Clipboard with WSL2 and remote hosts

  1. How to copy from vim to windows clipboard?
  2. How to copy from tmux to windows clipboard?
  3. How to copy from remote tmux/vim to windows clipboard?

Install VcXsrv

Config on host

$ sudo vim /etc/ssh/sshd_config
X11Forwarding yes
X11UseLocalhost no
$ sudo service sshd restart
$ sudo yum install xauth xclip

Config on WSL2

$ sudo yum install xauth xorg

$ vim ~/.bash_profile
LOCAL_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
export DISPLAY=$LOCAL_IP:0
# without VcXsrv
alias pbcopy=clip.exe
alias pbpaste="powershell.exe -Command get-clipboard"
# with VcXsrv
alias pbcopy=xclip
alias pbpaste="xclip -o"

$ vim ~/.ssh/config
Host host
     User user
     Port 22
     Hostname host
     IdentityFile ~/.ssh/id_rsa
     ServerAliveInterval 120
     ForwardAgent yes
     ForwardX11 yes
     ForwardX11Trusted yes
     XAuthLocation /usr/bin/xauth
     
$ xauth generate $DISPLAY .
$ ssh -X host

Tmux config

$ vim ~/.tmux.conf

setw -g mode-keys vi

bind-key -T copy-mode-vi v send -X begin-selection;

if-shell 'uname -a | grep -q Linux' " \
  bind-key -T copy-mode-vi y send -X copy-pipe-and-cancel 'xclip -i -sel clipboard'; \
  bind-key -T copy-mode-vi Enter send -X copy-pipe-and-cancel 'xclip -i -sel clipboard';"

if-shell 'uname -a | grep -q microsoft' " \
    bind-key -T copy-mode-vi y send -X copy-pipe-and-cancel 'clip.exe'; \
    bind-key -T copy-mode-vi Enter send -X copy-pipe-and-cancel 'clip.exe';"

if-shell 'uname -a | grep -q mac' " \
    bind-key -T copy-mode-vi y send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy'; \
    bind-key -T copy-mode-vi Enter send -X copy-pipe-and-cancel 'reattach-to-user-namespace pbcopy';"

References

https://serverfault.com/questions/422908/how-can-i-prevent-the-warning-no-xauth-data-using-fake-authentication-data-for https://sanctum.geek.nz/arabesque/reloading-tmux-config/

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