Making My Own Clipboard
Last weekend, I made a private clipboard. You can play with it here and I explain the process of making it in this blog!
⌘+C. ⌘+V. I do this way too many times a day, at an instinctual level. I don’t know when I learned how to copy and paste, but it certainly feels like I’ve known it all my life. The clipboard is probably the most widely used shortcut on the keyboard.
But how does it really work?
How The Clipboard Works#
The clipboard is, in essence, some memory space on the RAM that stores information temporarily. You can think of it as a massive cache.
How massive? The memory allocation function the clipboard calls can get up to 4GB blocks. I’ve found no documentation suggesting that there can’t be multiple blocks. So, the amount of data available is the available space in our virtual memory –– a seemingly infinite sea of space.
You could copy the entire works of Shakespeare onto the clipboard and it wouldn’t even bat an eye.
Clipboard Preserves Formatting#
The clipboard is deceptively simple. Ever wonder how the clipboard can save images, text, files, emails?
The clipboard is designed to recognize and hold many different types of data. Text is held in the (appropriately named) text format, images in the TIFF format, and so on. When copying a local document, it does not copy over the document itself but a reference to the item itself.
The clipboard is also careful to preserve these formats in some places while discarding them in others. Text pasted in a markdown file is unstyled, while that in a word document is left styled. Images can’t be pasted in the markdown file, but are easily added to the word doc. These decisions are made by the clipboard.
Searching for Documentation#
There is painfully limited documentation available on how the clipboard works, most of it linked below. In essence, both Apple and Microsoft describe a few functions that dictates the clipboard’s actions (
Close) and some notes about how best to utilize the clipboard when coding functionally.
Making My Own Clipboard#
When finding ways to manipulate the clipboard seemed difficult to find, I started wondering what it would take to make a simple clipboard, without the formatting complications.
A Clippy, if you will.
My core realization was that, mathematically, the clipboard was a simple state variable updated based on some condition. In a lot of languages, this is as simple as a one line declaration. The power of the clipboard is its ability to work across platforms.
Clippy, however, needs to do no such thing. Clippy could remain isolated to its project, without worrying about interacting with global memory.
You can try it out yourself at clippy.amks.me or check out the shockingly light weight repo. One clipboard down, everything else in the CPU still left to do!