Search code examples
emacsosx-lionpasteemacs22

How to paste to Emacs from clipboard on OSX?


This might be extraordinarily simple, but I am playing with Emacs (22.1.1) and I can't get it to paste text in the clipboard using Control-Y.


Solution

  • If you're using the in-built Emacs, then you're running Emacs in the terminal. The "clipboard" is a function of your windowing system. Emacs in terminal mode (-nw) does not access any windowing system specific APIs. This is true of most command line tools designed to work in the terminal.

    You need to upgrade your Emacs as others have suggested, and run in graphical mode. Using Emacs 24 on Mac OS X, the behaviour you want is the default.

    If you want to do this in a terminal, then this hack will make the clipboard work.

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