33

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?

Stefan
  • 26,154
  • 3
  • 46
  • 84
Galder Zamarreño
  • 1,527
  • 2
  • 12
  • 21

7 Answers7

38

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)


Explanation:

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.

31

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)
oLas
  • 113
  • 7
Galder Zamarreño
  • 1,527
  • 2
  • 12
  • 21
9

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.

Stefan
  • 26,154
  • 3
  • 46
  • 84
  • Does `xclip` work with OSX? From what I've seen out there, it seems to be designed to work with X11, see [post](http://www.lingotrek.com/2010/12/integrating-emacs-with-x11-clipboard-in.html) – 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 01 '18 at 12:25
  • Could you report it as a bug (either directly to me or via `M-x report-emacs-bug`? – Stefan Sep 01 '18 at 15:22
4

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 ()
  (interactive)
  (let ((deactivate-mark t))
    (call-process-region (point) (mark) "pbcopy")))

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

(defun pbcut ()
  (interactive)
  (pbcopy)
  (delete-region (region-beginning) (region-end)))
nisetama
  • 241
  • 2
  • 4
  • 1
    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
1

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 ()
(interactive)
(shell-command (concat "cat " (buffer-file-name (window-buffer (minibuffer-selected-window))) " | pbcopy")))
jumar
  • 109
  • 1
  • 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 01: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 05:55
0

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.

bitops
  • 333
  • 1
  • 12
0

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 ()
    (interactive)
    (setq interprogram-cut-function 'paste-to-osx)
    (setq interprogram-paste-function 'copy-from-osx))
  (defun clipboard-off ()
    (interactive)
    (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))
achalk
  • 579
  • 4
  • 16