Jul 13,
2019

Weekly Yak Shaving

Ahoy Emacs aficionados.

I don't know if it's a common experience to other Emacs' users, but often in my daily usage of the programmable editor, I became aware of a missing feature, I take a mental note that I should look into it later and usually I end up forgetting about it.

Rinse and repeat, but this time with a slightly sense of deja-vu.

Anywho, a few days ago, after a lot of postponement, I recalled that I wanted to expose the X's primary selection via a keyboard shortcuts on Emacs. For the non initiated, Xorg1 traditionally uses two distinct buffers for copy/paste text operations: the "primary selection" and the "clipboard".

In Emacs, from what I know, the only way to paste the primary selection is clicking the middle mouse button. Well, hidden in the documentation there's the gui-get-primary-selection function, but strangely it doesn't work when invoked directly with a shortcut. It's not a big deal, but that's probably the reason why I wasn't bored enough to fix it yet.

So nothing earth shattering, it's only a wrapper and it's not going to increase my productivity tenfold, but it was a pet peeve of mine. For the interested reader:

;; paste X primary selection
(defun yank-primary-selection ()
  (interactive)
  (insert
   (gui-get-primary-selection)))

(global-set-key (kbd "C-c C-y") 'yank-primary-selection)

Unfortunately, outside Emacs there isn't a common easy way to access the primary selection without applying brittle hacks like, for example, simulating a middle mouse button click sending the corresponding X11 event and this ignoring Wayland2 completely.

A little digression

I suppose for consistency with other desktops/operating systems, Shift-Insert is configured to paste the copy buffer and this is honored, I think, in all the applications I use, except for Gnome Terminal. In Gnome Terminal Shift-Insert paste the primary selection.

I tried in the past to add Shift-Insert to the default Control-Shift-v in the Gnome Terminal as a workaround, in other words having two shortcuts for the same action, but I doesn't seem to work. I tried to define an array with gsettings with the two bindings but only the first element of the array seems to be evaluated.

Bummer.

Another little digression

My default keyboard3 has a spare key in the top left corner, under the Esc key, so long time ago I mapped the "XF86Copy" and "XF86Paste" symbols to it. Unfortunately they aren't supported consistently on all applications as they work with Firefox for example, but I don't recall any Gnome or GTK based application where they do.

In Emacs I've added a couple of bindings, but I used to the traditional yank and kill4 shortcuts:

;; adding support for XF86Copy and XF86Paste
(global-set-key (kbd "<XF6Copy>") 'clipboard-kill-ring-save)
(global-set-key (kbd "<XF6Paste>") 'x-clipboard-yank)

To be honest, the configuration is there but I seldom use it.

The Shell

Now, for something slightly related, when using the shell and the result of a command is a single or at most a couple of lines, I can use it as an argument for another command using the following Zsh function instead to reach for the mouse to do a copy&paste:

# quote the last command to reuse output
_insert-last-command-output() {
    LBUFFER+="$(LS_COLORS= TERM=vt220 eval $history[$((HISTCMD-1))])"
}
zle -N _insert-last-command-output
bindkey '^[`' _insert-last-command-output

It's bound to Alt-` in my .zshrc and basically it relaunch the last executed command present in the shell history, with a couple of incantations used to remove colours from the output (a.k.a. ANSI escape sequences), and it injects it the Zsh input buffer. I don't recall where I "borrowed" the idea for the above function, but it has been handy plenty of times.

Finally, the Zsh copy-earlier-word is another pretty unknown little function that I've bound it to Alt-, that I want to throw to the interested reader. It treats the input buffer like a stack and pops out the current inserted words and it also works in the history parameters substitutions.

We should chalk it in the things Bash sorely misses.


  1. the free software implementation of the display server for the X Window System, in other words the thing that draws graphics on your Linux distribution ↩︎

  2. Wayland is a communication protocol that aims to replace the X Window System ↩︎

  3. three according to the ICCCM documentation, even if I never found how to use the secondary selection ↩︎

  4. What has two thumbs and an Ergodox ↩︎

  5. the Emacs user knows what I'm talking about, for the others not yet converted yank is paste and kill is cut ↩︎