Apple - Configure emacs to cut and copy text to Mac OS X clipboard

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)

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.


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.