17

Can I configure emacs, so that I could copy/cut text from emacs (default Terminal version) and paste it somewhere else (into X Window program)?

1
  • Nitpick: you say "default Terminal version", but the default mode of functioning for Emacs is to use the GUI rather than the text terminal. Maybe you're referring to the ancient /usr/bin/emacs executable that's bundled by Apple with OSX and which is arbitrarily limited to the text terminal?
    – Stefan
    Commented Jan 18, 2018 at 1:03

7 Answers 7

5

To copy the current region to the OS X clipboard, you can use

(shell-command-on-region (region-beginning) (region-end) "pbcopy")

The inverse, copying the OS X paste buffer to the Emacs kill ring, is not recommended. Kill rings and clipboards are very different structures and there's no good answer to what, exactly, pushing a clipboard into a kill ring should do. Still, you can run pbpaste manually to get the current clipboard contents.

1
  • This didn't work for me. The marked solution in this Emacs post does work however. Commented Apr 27, 2015 at 16:02
11

Add this to ~/.emacs:

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

(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)))

(global-set-key (kbd "C-c c") 'pbcopy)
(global-set-key (kbd "C-c v") 'pbpaste)
(global-set-key (kbd "C-c x") 'pbcut)

If you use Emacs in iTerm 2, you can also remap key combinations in the preferences:

Then add something like this to ~/.emacs:

(global-set-key (kbd "<f13> c") 'pbcopy)
(global-set-key (kbd "<f13> v") 'pbpaste)
(global-set-key (kbd "<f13> x") 'pbcut)
1
  • Still the best answer. Thanks! Note for iterm, to create these "Actions" choose "Send escape code," and you omit the leading "^[." (didn't work with "Send text" action type)
    – Jay
    Commented Dec 15, 2016 at 21:29
3

Here's a solution to integrate OS X Clipboard to Emacs' kill-ring by Daniel Nelson: https://github.com/wesen/emacs/blob/master/pbcopy.el

Author's comments:

Enables the kill-ring to interact with the clipboard when running Emacs from a Mac OSX terminal (without losing full kill-ring functionality). All I did was modify xclip.el to work with pbcopy and pbpaste. The real thanks go to Leo Shidai Liu, the author of xclip.el.

1

The following just works, got it 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))
1
  • Could you add a link to the original post?
    – Vincent
    Commented Apr 27, 2015 at 19:42
0

The version of emacs that is included with OS X is too old to provide this functionality.

Fortunately you can update emacs to a version that is integrated with your clipboard here: http://emacsformacosx.com

I believe that you use Ctrl+Y to paste in emacs, after the update.

5
  • Ah - I forgot to say - the version of emacs that you'll download from that site is graphical (i.e. it doesn't run in the Terminal). It's impossible for applications in the Terminal to access the clipboard without a hack, which I can't find anymore. :/ Commented Mar 15, 2013 at 21:35
  • 2
    pbcopy and pbpaste still exist and are not hacks.
    – user36802
    Commented Mar 15, 2013 at 22:59
  • That is possible, however I assumed that the OP wants to use keyboard commands to copy and paste. What I meant to say was that Terminal applications can't use keyboard shortcuts to copy and paste along with the OS X clipboard without hacks. Commented Mar 15, 2013 at 23:13
  • "Terminal applications can't use keyboard shortcuts to copy and paste along with the OS X clipboard without hacks." This is obviously not true, since pbcopy and pbpaste are both terminal applications; any other terminal app could do what they do, or simply call them directly.
    – user36802
    Commented Mar 16, 2013 at 1:28
  • What I was looking for is hooking pbcopy to Emacs cut & copy. Is it possible?
    – syntagma
    Commented Mar 16, 2013 at 8:44
0

I found this post really useful (defining 2 functions and assigning shortcuts to them in your ~/.emacs file).

After adding those lines to your .emacs file, you won't have access to those shortcuts until you open a new shell session (tab/window). Let the changes be applied.

0

You can get such interaction with the clipboard transparently while running Emacs in the text terminal by enabling the xclip-mode provided by the xclip package which you can install from GNU ELPA (i.e. via M-x package-list-packages RET).

See also https://emacs.stackexchange.com/questions/10900/ for a similar question&answers.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .