Numeronymize
July 5, 2024 at 5:07 PM by Dr. Drang
Anil Dash quote-tweeted this post from Ian Brown on Mastodon this morning:
I was so excited to learn that abbreviations like “a11y” or “c14n” are called “numeronyms” that I wrote an #Emacs^H^H^H^H^H^H #E3s extension to make it easier to use them.
The tweet1 links to this GitHub page. First I thought it was funny to see a link to an Emacs Lisp script the day after I posted about my date-convert
script. Then I thought “I want to be able to do that, but not in Emacs.” So I built this Keyboard Maestro macro, which I also called Numeronymize:
If you download and install it, it will appear in your Global Macro Group.
Here’s how to use it: Type the word you want to numeronymize in any editable text field. For example,
accessibility
With the cursor blinking at the end of the word, type ⌃⌥⌘3 (on standard American keyboards, the number symbol, #, is on the 3 key). The word before the cursor will be selected and shortened to its numeronym,
a11y
Honestly, I don’t think I’ll be using this macro very much, but it was fun and easy to write. The key is the Perl one-liner in the third step:
/usr/bin/perl -C -pe 's/(.)(.+)(.)/$1 . length($2) . $3/e'
I have more than one Perl executable on my computer, so I’m being explicit here about calling the one in /usr/bin
that comes with macOS. The -C
switch tells Perl to treat the input and output as UTF-8 (more on this below); the -p
switch tells it to loop through the input, apply the code to it, and print out the result; and the -e
switch tells it to treat the following string as the code to execute:
s/(.)(.+)(.)/$1 . length($2) . $3/e
This is the cool part, because Perl’s substitute command has an e
option that means “evaluate.” It treats the replacement as a chunk of Perl code, evaluates it, and returns the result. Here, it concatenates the first letter, the length of the middle string of characters, and the final letter. When I saw what Brown’s numeronymize extension did, I immediately thought of this feature of Perl and knew I could do it in a one-liner.
The -C
switch isn’t needed if all we care about are words made of ASCII characters. But what if we want to shorten this?
streichholzschächtelchen
Without the -C
, we’d get
s23n
because the length
function normally returns the length in bytes, and the ä
takes up two bytes. But with the -C
, length
understands that we want characters, not bytes, so the macro returns
s22n
which is what we want.
A couple of other notes:
- The macro starts out by simulating the ⌥⇧← keyboard combination to select the word to the left of the cursor. My thinking was that I’d most often use this macro right after typing or pasting a long word. So it made sense to have the macro do the word selection this way. If you think it would be better to do the selection yourself, delete this first step.
- Because I use the clipboard to copy the long word and to hold the shortened numeronym before pasting, two items are added to Keyboard Maestro’s clipboard history. But because they’re just temporary items, I don’t think they belong in the history, so the last two steps in the macro delete the most recent two items in the clipboard history.
Thanks to Ian Brown for making the Emacs extension and to Anil Dash for bringing it to wide attention. Dash says his favorite numeronym is “e13n,” but I think he’s leaving out one of the T’s.
-
Yes, I’m using “tweet” and “retweet” to refer to posts on Mastodon. Since Twitter has given up its name, I feel these words are now fair game for any Twitter-like service. After all, they were invented by the Twitter users, not the company. ↩