A few years ago I wrote about a setup I coded for myself for when I want to make paying my bills a tiny bit less painful. One thing I mentioned then was the primary selection. For those of you who don’t know it: under X.org, you can select a portion of text (for example by dragging the mouse over it) and then paste it in the same or another application with clicking the middle button. It is extremely useful for quick copy-and-paste operations where you do not have to issue a separate “copy” command (usually C-c, but often also found on mouse context menu). And by the way, Wayland also supports this.
Sometimes I find it useful to use it even without the mouse. Everybody knows how to select text using keyboard – one common way is by using movement keys with shift, but the Emacs way is to activate the mark with C-SPC and move the point using any of multiple ways Emacs gives us. The question is, however, how to paste the selected text without touching the mouse?
Well, it’s Emacs, so it’s enough to press C-h c and click the middle button to find out. It turns out that the command is mouse-yank-primary. Despite its name, it can be invoked without a mouse, either via M-x or by binding it to some key.
One important thing to mention here is that there is an option called mouse-yank-at-point. If it is nil (the default), the primary selection will be pasted in the place you clicked the mouse. I have it set to t, which means I can click anywhere in the Emacs frame and the primary selection will be pasted at point.
Of course, this is not going to be useful a lot of the time since the usual workflow with the primary selection is to both select and paste the text with the mouse, but if you need to do using your keyboard or from Elisp code, you know now how. (In fact, if you need to use the primary selection in Elisp code, you might want to check out the gui-get-primary-selection function, too.)
That’s it for today, see you next week!