Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs 25 yank from x windows PRIMARY clipboard buffer with keyboard

Tags:

emacs

x11

xorg

Using Emacs 25 in a linux environment, I often copy text with the mouse and wish that I could paste the copied text with some command in Emacs, but currently the only way I know of is via the mouse middle click which is bound to mouse-yank-primary.

I've attempted to bind this to a key command, along with setting mouse-yank-at-point set true, but this (as I suspected) requires a mouse event to work correctly and I'm not sure how to get Emacs into believing that a mouse event went off due to a keystroke.

Anyone have any ideas? Or simply know the correct way to yank with the keyboard from the PRIMARY selection?

like image 934
Silfheed Avatar asked Feb 09 '15 05:02

Silfheed


4 Answers

After looking around thanks to Christian's answer, I found select.el and came up with the following to stick into my .emacs

;; Pull from PRIMARY (same as middle mouse click)
(defun get-primary ()
  (interactive)
  (insert
   (gui-get-primary-selection)))
(global-set-key "\C-c\C-y" 'get-primary)

Edit: As noted by Stefan, gui-get-primary-selection (and more generically, gui-get-selection) are only available in Emacs 25 and up. In Emacs 25.1 x-get-selection was made obsolete.

like image 79
Silfheed Avatar answered Oct 04 '22 00:10

Silfheed


I just got annoyed by emacs default behavior of inserting the secondary X-selection on S-insert and found this thread. I tried to use the code from Silfheed but emacs 24 has no function like 'gui-get-primary-selection'. So I browsed the source for 'mouse-yank-primary' and came up with this alternative solution:

;; Pull from PRIMARY (same as middle mouse click)
(defun paste-primary-selection ()
  (interactive)
  (insert
   (x-get-selection 'PRIMARY)))
(global-set-key (kbd "S-<insert>") 'paste-primary-selection)

So s-insert will insert the primary X-selection on the cursor position - just as in xterm...

like image 31
Stefan Avatar answered Oct 03 '22 23:10

Stefan


Try setting this:

(setq select-enable-clipboard t)

this way the normal kill/yank commands (eg C-w and C-y) will work with the clipboard. Works both on X11 and OSX (and, I believe, Windows as well).

If you consult the documentation for that variable (for instance via C-h v) you should a sentence like this:

You can customize this variable.

where "customize" is a link you can click. This will bring you to Emacs' customaization system which provides an easier and more guided way of configuring Emacs. In particular, it will show you at lot about the controls that may be relevant to tweak. Even you do not want to control your confuguration that way, you can use it as guide to important variables to set and what they can be set to.

like image 26
Christian Lynbech Avatar answered Oct 03 '22 23:10

Christian Lynbech


Hopefully this helps. It is shamelessly copied from above, but works both on 24 and 25. I have not tested in in other versions.

(if (< emacs-major-version 25)This w
    ;; in emacs 24 or previous
    (defun paste-primary-selection ()
      (interactive)
      (insert (x-get-selection 'PRIMARY))
      )
  ;; in emacs 25 and above
  (defun paste-primary-selection ()
    (interactive)
    (insert (gui-get-primary-selection)))
  )
(global-set-key (kbd "S-<insert>") 'paste-primary-selection)
like image 22
shu Avatar answered Oct 04 '22 00:10

shu