The reliable way to access system clipboard from Emacs

  |   Source

CREATED: <2013-05-31 Fri>

UPDATED: <2015-11-20 Fri>

The Emacs clipboard questions has been asked so many times. Yet few give a complete and reliable solution.

Only a dedicated project maintained by professional developer could solve this issue once for all.

simpleclip is such a project.

I only use its APIs `simpleclip-get-contents` and `simpleclip-set-contents`.

Here is my setup:

(require 'simpleclip)
(defun copy-to-x-clipboard ()
  (interactive)
  (let ((thing (if (region-active-p)
                   (buffer-substring-no-properties (region-beginning) (region-end))
                 (thing-at-point 'symbol))))
    (simpleclip-set-contents thing)
    (message "thing => clipboard!")))

(defun paste-from-x-clipboard()
  "Paste string clipboard"
  (interactive)
  (insert (simpleclip-get-contents)))

;; Press `Alt-Y' to paste from clibpoard when in minibuffer
(defun my/paste-in-minibuffer ()
  (local-set-key (kbd "M-y") 'paste-from-x-clipboard))
(add-hook 'minibuffer-setup-hook 'my/paste-in-minibuffer)
Comments powered by Disqus