phiffer.org

Dan Phiffer Dan Phiffer is an Internet enthusiast based in Troy, NY

Escape from distractionland

I recently added some scripts to my work laptop designed to help me break out of my reflexive “cmd-T, T, enter” keyboard habit. That keyboard sequence loads up my Twitter timeline in a new tab before I’ve even realized what’s happening. I’m untraining myself out of habitual social media grazing by enforcing a rigid schedule.

Based on Mike Rugnetta’s excellent write-up, I basically hijack my Mac laptop’s /etc/hosts file on a daily interval from 10am to 6pm, with a one hour lunch break at noon. I’ve modified his approach slightly, since I tend to edit my /etc/hosts file regularly for web development, and can’t be bothered to maintain two separate copies of it. Instead of swapping between two different hosts files, I use sed to modify the file in place.

In my hosts file I have a couple lines that look like this at the beginning of the day:

# distractionland
# 127.0.0.1 twitter.com mobile.twitter.com api.twitter.com instagram.com mltshp.com

The rule starts the day commented out, so up until 10am it’s still open season on my wandering attention. Notice that you can stack up a bunch of hostnames after the IP address, there’s no need to make a separate line for each one. I recently deactivated my Facebook, so I don’t need that one in the mix any more.

My first script /usr/local/bin/disable-distractions blocks access to Twitter, et al by uncommenting the line:

#!/bin/sh
sudo sed -i .bak -E 's/^# (127.0.0.1 twitter.com.+)$/\1/' /etc/hosts

The script creates a backup file hosts.bak and then removes the comment like this:

# distractionland
127.0.0.1 twitter.com mobile.twitter.com api.twitter.com instagram.com mltshp.com    

The second script /usr/local/bin/enable-distractions comments the line back out, unblocking the websites:

#!/bin/sh
sudo sed -i .bak -E 's/^\s*(127.0.0.1 twitter.com.+)$/# \1/' /etc/hosts

The -i flag for sed is for “inline” editing, and the -E activates extended regular expression syntax. The edit command uses the form 's/.../.../', which basically reads as “search for … and replace it with …” The first … is a regular expression matching the line, and the second … either prefixes the line with # or removes the #.

I use the launchd daemon to call these scripts using four .plist files according to a daily schedule:

  • 10am: disable-distractions
  • 12pm: enable-distractions
  • 1pm: disable-distractions
  • 6pm: enable-distractions

Note that the computer must be running at each transition time, or the scripts won’t fire. But I can always just invoke one or the other from the command line if needed.

The launchd configuration is handled by four .plist files saved in my /Library/LaunchDaemons folder. Once everything was set up I activated them like this:

sudo launchctl load /Library/LaunchDaemons/org.phiffer.workday-start.plist
sudo launchctl load /Library/LaunchDaemons/org.phiffer.workday-end.plist
sudo launchctl load /Library/LaunchDaemons/org.phiffer.lunchbreak-start.plist
sudo launchctl load /Library/LaunchDaemons/org.phiffer.lunchbreak-end.plist

I’ve found this “one simple trick” has been effective at managing my novelty-seeking brain. Each time I absent-mindedly load up a blocked page I take a deep breath and close the tab. For now I’ve chosen not to block my feed reader, so I’m keeping up with the weblogs I subscribe to much more regularly than I did before. Keep on posting, friends!