Copy paste in tmux session inside ssh
2021-07-31

Copy paste in tmux session inside ssh

When you access your tmux session on a remote machine from different client machines, based on the client machine configuration and terminal, etc., some features will not work. One of them is copy-paste.

I used to have tmux-yank configured to xclip. But it didn’t work will when I accessed my remote VM from a Windows machine running putty. Similarly I had to configure every new machine I start using which was time consuming. Majority of my copy-paste activities will be between tmux panes and windows. I don’t usually copy from the local machine to the remote machine. So I used this simple hack.

Configured my .tmux.conf to redirect yank-ed to a temporary file as below.

1
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'cat > /tmp/clipboard'

Configured .zshrc to load an environment variable from that temporary file on every prompt.

1
precmd() { export p=`cat /tmp/clipboard` }

So if I copy any text in tmux using y, it will be populated into a environmental variable named p.

NOTE: As the environmental variable will be loaded upon next prompt only, after copying, you need to press enter once it to take effect

Below gif will show how it works
tmux copy paste hack