Reading time in TextMate

Several people in the Mac blogging world have mentioned how much they like iA Writer’s reading time estimate. I have no interest in iA Writer, but the reading time estimate sounds like a good idea. I decided to add a quick command to TextMate to put the reading time estimate in a popup tooltip.

There’s not much to this command. The basic idea is to count the words in the document and divide by the average reading speed. The word count is done by the venerable Unix utility wc, and the average reading speed is taken to be 250 words per minute, which is in the range of values I found in Wikipedia and other online sources. I don’t know, but I’d guess that iA Writer is using either that figure or a value close to it.

Here’s the command, as shown in TextMate’s Bundle Editor:

Reading time TextMate command

The pipeline in the Command(s) field is this:

wc  | awk '{print int($2/250) ":" int($2%250/250*60)}'

wc  | awk '{printf "%d:%02d", int($2/250), int($2%250/250*60)}'

The wc command returns the word count as the second item of output, which is why you see $2 in the awk command. The hardest part is the gymnastics in awk that turn decimal minutes into minutes and seconds.

If text is selected, this command shows the reading time for that text; otherwise it shows the reading time for the document as a whole. I have the Key Equivalent set to ⌃⌥⌘R, which is easy to type and easy to remember. I tend to use the Control, Option, and Command modifiers for all my commands because those keys are lined up next to one another and are easy to press simultaneously.

For what it’s worth, this post comes in at 1:16.

Update 6/4/11
Reader Shawn Medero in the comments pointed out an error in my pipeline, and I’ve made the correction in the text above (the screenshot still has the error). My mistake was not to ensure a leading zero after the colon if the number of seconds was less than 10. In the original, a reading time of 3 minutes, 7 seconds would be shown as 3:7 instead of 3:07.

This particular error in formatting minutes and seconds is one I’ve made so many times that I suspect there’s some defect in my brain that’s preventing me from ever learning from the mistake. Since the error manifests itself on the average once every six times the script is run, I usually catch it pretty quickly. This time Shawn caught it for me.

Shawn also describes how to use Automator to turn this into a system-wide service instead of a TextMate-specific command. I’m beginning to think the post would have been much better if he had written it instead of me.

Now it’s 2:05.

Update 6/5/11
See this post for an improved TextMate command, a Service, and a Safari extension.


6 Responses to “Reading time in TextMate”

  1. Shawn Medero says:

    A slight improve to this function would be to use printf to pad the seconds with a leading 0 if necessary:

    wc  | awk '{printf "%d:%02d\n", int($2/250), int($2%250/250*60)}'

    FWIW, your trick works greats as an OS X service built in Automator using the “Run Shell Script” action. Now you can use this in TextEdit, BBEdit, a random textarea in Safari or anywhere that has text and understands Services.

  2. Neil Gupta says:

    I’d love to see this integrated into Gmail so that it warns me if I’m sending an email that will take more than 30 seconds to read.

  3. Dr. Drang says:

    Neil,

    Sounds like an extension to me. I’m not a big JavaScripter, but I doubt it would be hard to whip up a JS function that returned the word count and/or estimated reading time when passed a string. The time-consuming parts would be

    1. Sifting through GMail’s DOM to find the input field.
    2. Updating the time with every keystroke. You could probably modify Dr. Twoot’s charCountdown function.
    3. Finding a spot in the DOM to insert a new field with the reading time.

    Let us know if you come up with something.

  4. Neil Gupta says:

    Good idea. I threw a quick Safari extension together that displays the reading time for Gmail. You can download the extension from here or get the source code from GitHub.

  5. Dr. Drang says:

    Neil,

    Nice work! Your placement of the reading time is perfect—it looks like it came from Google itself.

    Two suggestions:

    1. Shorten the message from “Email will take 0:25 to read.” to “Reading time: 0:25”.
    2. Reconsider turning the reading time red when the message exceeds 30 seconds. Brevity is admirable in general, but it’s not always possible or desirable.

    PS: I edited your comment @ 4 so the URL wouldn’t break through the width of my content section.

  6. Neil Gupta says:

    Thanks!

    Great suggestions. I have updated the extension to say “Reading time:” instead.

    I personally liked the red highlighting, but I definitely agree that brevity is not always possible, so I added an option in the Settings of the extension to control whether the highlighting shows and after what period it shows. By default, the feature is turned off.