Skip to main content

Enhance your writing with cURL

Motivation

Back in my school years, I developed a habit to use a dictionary while writing or reading more demanding texts. As a result, I managed to expand my vocabulary. Furthermore, Thesaurus allowed me to avoid repetitions in my texts. Consequently, they were more attractive to the readers and less boring to read. Finally, I’m not a native English speaker. The dictionary is still necessary for me to understand some more exotic words and prevents me from using them improperly.

Back in the old days, I used dictionaries in the forms of thick books, Longman Dictionary of Contemporary English would be one example. Nowadays, dictionaries are available online. There are plenty of websites that allow for word lookups. However, many of them follow the current Internet “trend” for serving the content in a bloated form, full of JavaScript, ads, and popups. In my opinion, the need to wade through cookie consents, notifications, and other hallmarks of the modern web is excessive when my sole purpose is to check the meaning of a single word.

All the above made me search for a dictionary solution that will be easy, fast, and close to the place my writing takes place: the terminal. As a result, I came up with a couple of scripts that allow me to perform word lookups directly in the command line. I achieved this by combining curl utility with DICT protocol.

The scripts

I managed to bring a dictionary to my terminal with several shell functions. Interacting with the DICT server is as simple as issuing a simple curl request:

$ curl dict://dict.org/d:blog

The above command will query dict.org: the service that aggregates several open dictionaries. The check word is the word blog. The server responds with the following text:

220 dict.dict.org dictd 1.12.1/rf on Linux 4.19.0-10-amd64 <auth.mime> <84520322.25555.1640205547@dict.dict.org>
250 ok
150 1 definitions retrieved
151 "blog" wn "WordNet (r) 3.0 (2006)"
blog
    n 1: a shared on-line journal where people can post diary
         entries about their personal experiences and hobbies;
         "postings on a blog are usually in chronological order"
         [syn: {web log}, {blog}]
    v 1: read, write, or edit a shared on-line journal
.
250 ok [d/m/c = 1/0/31; 0.000r 0.000u 0.000s]
221 bye [d/m/c = 0/0/0; 0.000r 0.000u 0.000s]

As you can see, the response contains the definition for the queried word and the control messages of DICT protocol.

To reduce the boilerplate to type for each lookup, I introduced a simple shell function:

dict () {
  db=$([ -n "$2" ] && echo -n ":$2" || echo -n "")
  term=$(echo -n "$1" | jq -Rr @uri)
  curl "dict://dict.org/d:$term$db"
}

If the second parameter is given, it is treated as an identifier of the dictionary. Otherwise, the server will search all available dictionaries for definition. The invocation is as simple as:

$ dict blog

The jq provides URI encoding of the given term. As a result, multi-word expressions can be searched for:

dict 'look up'

To list available dictionaries, I introduced the following function:

dicts () {
  curl "dict://dict.org/show:db"
}

As I mentioned, the thesaurus helps greatly in writing. I created shortcut to look up the synonyms of the given word:

thes () {
  dict "$1" moby-thesaurus
}

Finally, sometimes I need to translate a word from Polish to English (or vice versa). This functionality is provided by the two functions:

pol2eng () {
  dict "$1" fd-pol-eng
}

eng2pol () {
  dict "$1" fd-eng-pol
}

Conclusion

The most significant drawback of my dictionary solution is the fact that the protocol control messages interleave with the word definitions. In some cases, they cloud the relevant information a bit. The control sequences could be easily removed with tools like sed or grep. However, I decided not to put additional effort as the protocol information is bearable.

There are other DICT protocol clients. However, curl is a tool I use for other purposes as well and I always have it on my systems. Furthermore, it is fast and does not consume too many resources. It also suits the lookup use case well. It searches for a word, displays the definition, and goes away as I do not need it anymore (for now).