Ideas

The clipboard is a basic yet integral part of every environment. Over time, I have had quite a few ideas regarding clipboards. I have implemented the following ideas currently:

Clipboard Swap

Inspired by vim’s registers, I thought about implementing something similar on a system level for Xorg. My current implementation basically swaps the contents of the clipboard with another clipboard. Currently, the workflow looks something like this.

Implicit Copying

Some time ago, a realization hit me that I practically always select text for the sake of copying or cutting it. Cutting is just copying and then deleting that text. So everytime I select text, it is for copying it. Due to this, I implemented implicit copying where text is copied just by selecting it. So, the workflow for copying is reduced from select, ctrl+c, paste to select, paste while the workflow for cutting is same.

Clipboards in Xorg

In Xorg, clipboards are called selections. Xorg has three selections: Primary, Secondary and Clipboard.

Implementation

Secondary selection is the perfect selection to swap the contents of Clipboard selection with. It is practically never used so nothing will overwrite the contents of secondary selection.

Here is the clip_swap.sh which I have bound to Ctrl+` in my i3 config.

#!/usr/bin/env bash

x=$(xclip -selection secondary -out)
xclip -selection clipboard -out | xclip -selection secondary
echo "$x" | xclip -selection clipboard

For implementing implicit copying, I basically needed a daemon that watched for changes in primary selection and copies them over to clipboard selection. Here are the contents of clip_syncd.sh

#!/usr/bin/env bash
# Daemon to synchronize PRIMARY selection with CLIPBOARD selection

old=""
while :; do
	new=$(xclip -selection primary -out) || { sleep 0.5; continue; }
	if [[ $new != "$old" ]]; then
		xclip -selection primary -out | xclip -selection clipboard -in
		old="$new"
	fi
	sleep 0.5
done