Natalie Pendragon The Intergalactic Blog

Word Definition Popups

One thing I really like on osx, but miss on Linux, is the thing where you three-finger tap a word to look up its definition. It’s really nice to be able to get the definition so easily, without having to context switch, open up a new tab, or anything. So I set out to try and replicate the functionality myself!

It got me thinking about how to do something similar in Linux. I didn’t really want to start messing with touchpad stuff, so I thought I would try the ever-so-slightly-less-elegant workflow of double tapping to select a word on screen, ctrl+c to copy into the clipboard, then meta+c to get the definition as a os popup notification. The magic, of course, lies in the meta+c step, which is what I set out to find or build.

I found this project on github, which is essentially what I just described, and looks like a nice little Golang project. I thought I would create an alternative because that one is A) hardcoded to use xclip, and I wanted to use wl-clip, and B) I thought I could replicate the entire project with a couple lines of bash.

I had to spend a few minutes finally learning how to use jq, but after that it was relatively simple and I’m pleased with the end result. As long as you don’t go over 10k requests per month, the twinword api is free to use, and now you can get definitions easily!

#!/usr/bin/env bash

WORD=$(wl-paste)

curl -d "entry=$WORD" 'https://www.twinword.com/api/v4/word/definition/' | jq '.meaning | map_values(select(length > 0)) | join("\n")' | xargs notify-send --expire-time=10000 "$WORD"
  (menu-bar-mode -1)