How to paste locally a text copied remotely
For the last months I have been trying to use my linux computer without plugging a mouse. I guess it is a logical next step after deciding to use as few graphical applications as possible. Being accepted as a sundog in the new Soviet capsule of the Circumlunar Space is a great opportunity to exercise this recent resolution.
I almost always open my terminal emulators with a `tmux(1)` process already running (I have a global keyboard shorcut to exec `konsole -e tmux`), so I have some experience using a terminal multiplexer in my local machine. It has been a relatively easy task for me to use a pubnix system with only my keyboard and several applications opened, each in its own pane/window. That's something I like very much and feel comfortable doing.
But there are times when I find myself moving my hands looking for the mouse: to copy some text from the remote `tmux(1)` session on the pubnix to my local machine. I want to avoid this and keep them on my keyboard's home row! There must be a way!.
Yes, there seems to be a very easy way to avoid using the mouse in such situation. You only have to connect to the remote machine using the `ssh(1)` `-X` option to enable X11 forwarding, and be sure you have defined in your `.tmux.conf` file a `tmux(1)` keyboard shortcut to copy the selected text to your X11 clipboard. You can do this in two ways, depending on the version of `tmux(1)` you have:
bind -t vi-copy y copy-pipe 'xclip -in -selection clipboard'
for tmux versions < 2.5, or:
bind -T copy-mode-vi y send-keys -X copy-pipe-and-cancel 'xclip -in -selection clipboard'
for tmux versions >= 2.5.
So, basically, you can select and yank the text from your `tmux(1)` remote session, and magically it will be copied to your local X11 clipboard. Wonders of X11 forwarding and the client/server architecture of your graphics stack.
But, what can you do if your remote machine has X11 forwarding not enabled or no `xclip` command installed?
ssh soviet "tmux save-buffer -" | xclip -in -selection clipboard
If you run the above text in a local terminal, your clipboard will be filled with the last yanked text in your remote `tmux(1)` session. Let's see what's happening with this command.
Basically we have two commands here. One is executed in the remote machine via `ssh(1)` and the other is executed in the local machine. Both commands are connected using a pipe (`|`), in such a way that the oputput of the first command is used as the input to the second one.
The command executed in the remote machine is enclosed in quotes. This command has the following general form:
tmux save-buffer -b
and it saves in the file <path> the `tmux(1)` buffer named <name>. We are not interested in saving the contents of a paste buffer in a file, we only want to print it to standard output, so we use the `-` character as <path>. If we omit the name of the paste buffer, the last one is used, so we don't need the `-b` option.
We just have to copy the extracted text to our primary clipboard. That's the command executed in our local machine:
xclip -in -selection clipboard
Now we have the wanted text available to paste it where needed!
Obviously you are not going to type that long command every time you want to yank locally a remote text, so you can assign it to a global keyboard shorcut easily accessible, *CTRL+ALT+C* in my case. I can do *CTRL+ALT+C* in my local machine to get a copy of a remote content, and then I can *CTRL+V* to paste it where desired. But don't forget to yank the text in a `tmux(1)` paste buffer before doing the remote copy in your local machine.
And that's all folks, one less excuse to need a mouse :)
~~o