When I run Emacs in a text terminal instead of the normal GUI mode, I can copy in Emacs by activating the start of a region with C-SPC, and then select what I want, and then do M-w.

Is there any way to get a region copied this way be available in the OSX clipboard to paste it say in a web page (other than by running Emacs in its GUI mode, obviously)?

I'm not interested in creating a different shortcut to do that, and ideally it should not require installing any modules.

I've seen this post and tried the accepted answer but it does not work seem to work the way I expect above.

Any other ideas?

  • Dude, the question is almost 3 years old and it has been answered – Galder Zamarreño Jan 18 '18 at 9:07

Copy from Emacs to OS X clipboard:

select region then M-| pbcopy RET

Paste from OS X clipboard to Emacs:

C-u M-| pbpaste RET (replaces current region if it exists)


M-| runs shell-command-on-region, which as the name implies pipes the current region to a shell command. C-u M-| does the same thing, but replaces the current region with the stdout of the command being run.

pbcopy and pbpaste are OS X utilities for accessing the system clipboard.

  • 1
    Besides answering the question, your answer taught me how to run shell commands from emacs, Amazing. – andreskwan Oct 4 '19 at 4:15

The following just works, copied from here

(defun copy-from-osx ()
  (shell-command-to-string "pbpaste"))

(defun paste-to-osx (text &optional push)
  (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
      (process-send-string proc text)
      (process-send-eof proc))))

(setq interprogram-cut-function 'paste-to-osx)
(setq interprogram-paste-function 'copy-from-osx)
  • The verbiage here seems backwards – Jordon Biondo Apr 27 '15 at 19:47
  • 2
    With evil-mode, this breaks the vimish pasting behaviour – Felix D. Jul 5 '16 at 15:31
  • @FelixD.: does xclip-mode suffer from the same problem? – Stefan Jan 18 '18 at 1:11
  • This seems broken in Emacs26 when copying and pasting multibyte characters, as described in emacs.stackexchange.com/questions/50485/… – silencej May 13 '19 at 13:25

If you use Emacs "the normal way" (i.e. as a GUI application), then it should already do that by default. If you use Emacs inside a terminal emulator, then indeed it doesn't know how to do that by default, but you can install the xclip package from GNU ELPA and then enable the xclip-mode which teaches Emacs how to do that both for GNU/Linux and for OSX.

  • Does xclip work with OSX? From what I've seen out there, it seems to be designed to work with X11, see post – Galder Zamarreño Apr 27 '15 at 15:45
  • 1
    the xclip.el package has been extended to use pbpaste. – Stefan Apr 27 '15 at 17:41
  • terminal emacs in server mode (not as regular instance) freezes when I use xclip under macOS for some reason. – rien333 Sep 1 '18 at 12:25
  • Could you report it as a bug (either directly to me or via M-x report-emacs-bug? – Stefan Sep 1 '18 at 15:22

As Stefan said, the graphical Emacs applications should do that by default.

In a text terminal, another option is to use functions like this:

(defun pbcopy ()
  (let ((deactivate-mark t))
    (call-process-region (point) (mark) "pbcopy")))

(defun pbpaste ()
  (call-process-region (point) (if mark-active (mark) (point)) "pbpaste" t t))

(defun pbcut ()
  (delete-region (region-beginning) (region-end)))
  • except when it does not. 20 years later, same pb in emacs.... I really wish there was some common and sane ground, set in stone somewhere – nicolas Dec 14 '18 at 13:41

Here's a simple elisp function you can put in your .emacs file that copies the contents of the buffer you are in to the clipboard. You don't have to select anything, it copies it all. The code is short and pretty self-explanatory.

(defun xclip ()
(shell-command (concat "cat " (buffer-file-name (window-buffer (minibuffer-selected-window))) " | pbcopy")))
  • After you load or reload emacs call it with M-x xclip. – jumar Jan 17 '18 at 22:30
  • The OP didn't ask to copy the whole buffer to the clipboard, so this doesn't do what was asked. As for the rest, this seems to just duplicate what others have already said. – Stefan Jan 18 '18 at 1:07
  • I apologize if this seems off topic. I navigated to this page from Google, where it was the first result for "copy text to clipboard emacs." The question is titled "Copy text from Emacs to OS X clipboard", so it's easy to see how you can end up here for that search result. I do think it's applicable to the way the posted phrase their question, a superset of it. I've visited StackExchange answers before and been frustrated when a simple answer wasn't available, especially when I knew thousands of people were asking the question, so I thought I'd add mine for people who could benefit. – jumar Jan 18 '18 at 5:55

Check out clipboard-kill-region and clipboard-yank. These come from menu-bar.el.

Once a region is highlighted, you can use clipboard-kill-region to kill the region to both your paste buffer and the system clipboard. clipboard-yank "pastes" whatever is currently on the system clipboard.

  • Please expand on your answer. – Dan Apr 27 '15 at 0:40
  • I've tried to copy something from emacs using those commands, and was not available externally to copy it somewhere else outside of Emacs. – Galder Zamarreño Apr 27 '15 at 15:48
  • @GalderZamarreño it works for me in Emacs and I'm on OSX. – bitops Apr 27 '15 at 17:19

Galder Zamarreño's answer is great, however, it breaks the evil-like put behaviour in evil-mode. I've therefore adapted it a bit below—I'm sure it could be better, but it does the job for me. Feel free to suggest improvements:

(defun copy-from-osx ()
  (shell-command-to-string "pbpaste"))

(defun paste-to-osx (text &optional push)
  (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
      (process-send-string proc text)
      (process-send-eof proc))))

---->New Code

  (defun clipboard-on ()
    (setq interprogram-cut-function 'paste-to-osx)
    (setq interprogram-paste-function 'copy-from-osx))
  (defun clipboard-off ()
    (setq interprogram-cut-function 'gui-select-text)
    (setq interprogram-paste-function 'gui-selection-value))
  (global-set-key (kbd "C-c C-p") 'clipboard-on)
  (global-set-key (kbd "C-c C-y") 'clipboard-off))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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