Tiny bash script to copy kakoune selection to paste buffer on macOS

I am running kakoune under a tmux session in a normal macOS terminal. Because of that I cannot cut and paste to another window using the mouse or Command+C.

The following bash script, saved in my PATH as copy and made executable, allows me to store a selection in the macos paste buffer by doing |copy.

#!/bin/bash
x=$(</dev/stdin)
echo "$x"
echo "$x" | pbcopy

Kakoune is smart enough to see that the ouput is the same as the input to the script and therefor not mark the buffer as having changed because of this.

Even despite tmux and Kakoune handling mouse events, you should be able to select text in the terminal if you hold down Shift while dragging the mouse. Then Cmd+C should work as normal.

This works, but there is a simpler way: the <a-|> command, which :doc keys describes as:

pipe each selection through the given external filter program and ignore its output.

So if you do <a-|>pbcopy<ret> the selected text will be sent to pbcopy without having to pump it into an external process and then back into Kakoune.

Holding Shift while dragging the mouse doesn’t make a difference. It makes a selection in the same way that just dragging the mouse does. I can work on that selection in kakoune, but Cmd+C doesn’t do anything (and right-clicking does not bring up a mouse menu, so I can’t select copy, but changes the selection). I tried Shift+drag after starting kakoune with kak -n directly from the terminal, and could not get anything in the paste buffer in that case either. ( macOS on M1, installed with brew)

The <a-|> routine does work.

x=$(</dev/stdin)

note that this will strip trailing newlines. I use this trick to avoid the loss:

x=$(</dev/stdin; printf .)
x=${x%.}

Holding Shift while dragging the mouse doesn’t make a difference. It makes a selection in the same way that just dragging the mouse does. I can work on that selection in kakoune, but Cmd+C doesn’t do anything

I think you can disable “Mouse Reporting” to allow the terminal to make selections. (Personally I haven’t needed it since I fixed my OSC 52 copy script.)
When typing Command+C, iTerm2 actually asks you to (temporarily) disable Mouse Reporting, very nice UX :slight_smile: