3

Suppose you are working in macOS, and you decide to SSH into a Linux box and fire up terminal Emacs. How do you copy and paste between macOS and your SSH terminal-Emacs?

Copying and pasting between macOS and local terminal-Emacs is not a problem with xclip: install xclip through ELPA and put (xclip-mode 1) in your Emacs init file. This doesn't work when running terminal Emacs over an SSH session, however, and while there is much information online about accessing the X11 clipboard from Emacs, I am running terminal Emacs on a remote machine that doesn't have X11 installed either way. Is there a solution for terminal Emacs copy-paste over an SSH session?

  • I'm sadly not able to comment yet. If you use iTerm2 on macOS you should be able to use the OSC-52 escape sequence. I have not currently implemented the solution my self, but am looking at this article about using it for tmux and the principle should be the same. There is also a way with setting up a SSH remote tunnel and systemd service, but personally that s – ladrua Nov 8 '18 at 23:22
3

AFAIK there is no magic way to do that.

The closest thing I know uses the "OSC-52" xterm escape sequences. Emacs can use those if your xterm supports them, but AFAIK most builds disable them, partly because of security concerns.

See the values getSelection and setSelection in xterm-extra-capabilities. This support was new in Emacs-25, whose etc/NEWS file said:

*** Killing text now also sets the CLIPBOARD/PRIMARY selection
in the surrounding GUI (using the OSC-52 escape sequence).  This only works
if your xterm supports it and enables the 'allowWindowOps' options (disabled
by default at least in Debian, for security reasons).

Similarly, you can yank the CLIPBOARD/PRIMARY selection (using the OSC-52
escape sequence) if your xterm has the feature enabled but for that you
additionally need to add 'getSelection' to 'xterm-extra-capabilities'.
0

I have just implemented a solution using reverse ssh tunneling.

On your mac you need to enable Settings->Sharing->Remote Login.

I connect to the remote server using this command: ssh -R 1234:localhost:22 <user>@<remote-server> where 1234 is a available port on your server.

Be sure to copy your ssh key from the remote server to your macOS user, on ubuntu that can easily be done by running ssh-copy-id <macusername>@localhost:1234 after logging inn with the above command. This makes the solution work without having to enter you mac password everytime you copy.

In my .emacs file I add:

(defun write-region-to-client-clipboard (beg end)
  (interactive "r")
  (copy-region-as-kill beg end)
  (shell-command-on-region beg end "ssh -p 1234 <macusername>@localhost pbcopy" nil nil nil t))
(global-set-key (kbd "C-u") 'write-region-to-client-clipboard)

So now when I mark a region in emacs and hit Ctrl + u it copies the region to my macOS client's clipboard, as well as to my emacs kill-ring(clipboard)

This only works when copying within emacs, but could be setup to work from a remote tmux session as well.

  • That's very clever, but it would be great to not have to allow incoming connections on your local machine. – aparkerlue Nov 14 '18 at 18:51
  • Agreed, it's not ideal, but it works very well. And I normaly work on known servers in a closed private network, so that makes a difference. – ladrua Nov 15 '18 at 20:18

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.