The Fortunes File

5 minute read

Some months ago we talked about commonplacing, a technique for recording quotations and other random ideas you come across. In this rather short post, I’ll share an, if possible, even simpler tool that I use as a supplement: the fortunes file.

The idea

The purpose of a fortunes file is to resurface items from your commonplace or other sources in a compact format at random times. Seeing these fortunes is frequently entertaining and sometimes leads to serendipitous discoveries, and like the commonplace, it is almost laughably simple.

Each fortune in my system can be (by convention) up to 80 characters long (this means it fits across a standard-sized terminal without wrapping). Your system doesn’t have to have exactly the same limits, but these should be short: you should be able to read them in three seconds. Not every commonplace item is a good candidate for fortunifying, but many are; it sometimes takes a little rewording and poetic license to grab the most salient part and turn it into a fortune. Here’s a short snippet of my file (in my system, each line is one fortune):

Eyes Up, Devices Down
Thank the Lord for distributed version control.
They say I was at MIDI.
Oh. And "domain users" was a member of "domain admins."
They say that America is a moving target.
They say that I lost two dollars printing PDFs about Judaism.
"I will give you this nice new barometer if you will tell me the height."
They say that demonstrations involving your pants or fire are especially bad.
Walkmen!
Have you ever considered the possibility that everything went absolutely right?
"Wow, that's the second occurrence of the phrase 'llama teeth' in this file."
Patients can have as many diseases as they damn well please.
"Now only 50% off!"
The Requestedbee
They say that the hacker is causing your computer problems.
They say that Choral Day is like an aircraft carrier.
"Oh, I spilled soup on my keyboard. Does that make a difference?"
They say that bears do not use currency.

Like a commonplace, except even more so, many of these fortunes probably won’t make any sense to you, and that’s the point – they serve to return each person’s mind to odd things they still have significant memory of but might never think about again otherwise.

Since I’m a developer and use the command line a lot, I surface my fortunes every time I launch a new terminal window on my computer, which means I see 10–20 of these on a typical day. If this doesn’t work well for you, you could try using a service like Airtable or IFTTT to set up a workflow that emails you a couple of fortunes every morning, or come up with some other creative way to encounter them periodically. For this to work, you only need two things: (1) a collection of fortunes, and (2) a way to randomly pick one on a regular basis and show it to you.

My system

My fortunes file is a single text file called .fortunes in my computer’s home directory, and I have a simple Python script called get-fortune that picks a line at random to print:

#!/usr/bin/env python

import os
import random

with open(os.path.expanduser("~/.fortunes")) as f:
    fortunes = [i for i in f.readlines() if not i.startswith("#")]
print(random.choice(fortunes).strip())

A call to this script sits in my shell initialization file, .bashrc, so that it prints out as the first line of any new shell I open:

A new terminal window showing a fortune before the prompt.

I additionally have the concept of “private” fortunes. The lines of private fortunes begin with a single space. This has no effect at all when using the script above, since it does a .strip() to remove leading and trailing whitespace. The benefit here is that you can download your fortunes file onto a work computer or otherwise display fortunes in a more public place without having to explain weird and complicated fortunes to people who see them (although in my experience, people think just having these fortunes is weird and confusing – it’s a difficult concept to explain in the ten seconds you have to explain such things after you open a terminal window while working with someone!). Just modify line 7 of the script above as follows on workstations that shouldn’t show private fortunes:

    fortunes = [i for i in f.readlines() if not i.startswith("#") and not i.startswith(" ")]

Alternate versions

For those technical folks interested in optimizing this: the Python script, although simple, isn’t the simplest you can make this, and you might prefer not to need Python. Here’s a shell one-liner which you can drop right into your profile (drop a space after the # if you want to omit private fortunes from the pool):

shuf ~/.fortunes | grep -vm1 '^[#]' | sed 's/^ \+//

shuf requires GNU Coreutils; if you don’t have those you can use this delightfully obscure, fully POSIX-compatible hack:

awk 'BEGIN {srand(); OFMT="%.17f"} {print rand(), $0}' <~/.fortunes | sort -k1,1n | cut -d ' ' -f2- | grep -vm1 '^[#]' | sed 's/^ \+//

(Be aware that this last version will generate the same result if called multiple times in a second, as it uses the epoch time for the seed.)

D.K. suggests the following incantation to replace the final grep and sed steps in either version. This will be faster since it removes a pipeline step, but may be a little tricky to understand for those unfamiliar with sed:

sed '/^[#]/ d; s/^ \+//; 0 q'