cedaei.com/content/posts/ideas-from-my-dev-setup-cli...

89 lines
3.0 KiB
Markdown
Raw Permalink Normal View History

+++
title = "Ideas from my Development Setup: Clipboard"
date = "2020-10-07"
author = "Ceda EI"
tags = ["xorg", "linux", "development"]
keywords = ["xorg", "linux", "development"]
description = "Things with clipboards: Implicit copying and swapping"
showFullContent = false
+++
## 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.
- Copy text `abc`: Writes `abc` to the main clipboard.
- Press ``` Ctrl+` ```: Swaps content of hidden clipboard with main clipboard.
- Copy text `def`: Writes `def` to the main clipboard.
- Paste with `Ctrl+v`: Pastes text `def` as expected.
- Press ``` Ctrl+` ```: Swaps content of hidden clipboard with main clipboard.
- Paste: Pastes text `abc`.
### 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.
- Primary: Whenever text is selected, it is copied into primary selection. You
can paste contents of the primary selection by pressing middle mouse button
in most applications.
- Secondary: There is no consensus on what this is supposed to be used for and
hence it is used by practically nothing.
- Clipboard: This is the selection that practically every application interacts
with. Ctrl-C and Ctrl-V are commonly bound to writing to this 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.
```bash
#!/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`
```bash
#!/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
```