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
    Commented Nov 8, 2018 at 23:22

4 Answers 4


I just recently wrote an Emacs package called Clipetty which solves this very problem. It uses OSC-52 xterm escape sequences, is smart enough to know when you're running on a remote host, and can deal with both nested and non-nested terminal multiplexers like GNU Screen and Tmux. The README.md file explains in more detail how it works.

  • Thank you so much for writing this!! lifesaver.
    – RoyM
    Commented Jun 30, 2021 at 10:58

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'.

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
    Commented Nov 14, 2018 at 18:51
  • 1
    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
    Commented Nov 15, 2018 at 20:18

This is possible using Tmux and iTerm2. I am using Tmux version 3.0, iTerm2 version 3.3 and macOS Catalina. It probably works for a few earlier version but I have not researched it.

First, you need to enable iTerm2 to access the system clipboard. It is not enabled by default.

  1. iTerm2 -> Preferences -> Selection -> [check] Applications in terminal may access clipboard.

Then, from within a tmux session, ssh into your system and go to where you want to copy something.

  1. At this point you need to know how to enter Tmux's "copy mode".

    a. The default method is to use the key combination: prefix-[

    b. Now you need to know how to select what you want. Tmux's 'copy-mode' has two different methods depending on how you have set up your environment. One way is to use 'vi' key bindings; the other is to use 'emacs' key bindings. You either need to know which key bindings your system uses, or configure tmux to use the one you want.

    c. Assuming you know how to select, then you just need to paste, and the selection will be added to macOS's system clipboard.

Tmux's man page has information under the heading "Windows and Panes", then directly under that, 'copy mode'. Once you get the configuration and key bindings figured out, it is just a matter of copying and pasting from within iTerm2 inside a tmux session ssh'ed into your Linux box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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