Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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

share|improve this question

6 Answers 6

up vote 3 down vote accepted

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.

share|improve this answer
    
This didn't work for me. The marked solution in this Emacs post does work however. –  Galder Zamarreño Apr 27 at 16:02

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.

share|improve this answer
    
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. :/ –  Faiz Saleem Mar 15 '13 at 21:35
1  
pbcopy and pbpaste still exist and are not hacks. –  user36802 Mar 15 '13 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. –  Faiz Saleem Mar 15 '13 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 Mar 16 '13 at 1:28
    
What I was looking for is hooking pbcopy to Emacs cut & copy. Is it possible? –  REACHUS Mar 16 '13 at 8:44

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.

share|improve this answer

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)
share|improve this answer

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.

share|improve this answer

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))
share|improve this answer
    
Could you add a link to the original post? –  Vincent Apr 27 at 19:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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