Reclaim your focus with ~12 lines of bash
Computers are a tool that you can use or be used by. I have written about my tool-taming tactics before, including blocking distracting websites in my hosts file. My “checking” function is the natural evolution of that tactic.
It implements the core functionality of popular anti-procrastination apps like ColdTurkey, LeechBlock NG, Freedom, and Self Control in just ~12 lines of bash.
Here’s how to get set up with “checking”:
- Back-up your existing
/etc/hosts
file. - Rename it to
/etc/hosts-checking
. - Create a copy of it at
/etc/hosts-doing
, and add all the distracting websites to it. My personal kryptonite: Email, HackerNews, Lobste.rs, Website Stats, and Spotify for Artists for my band. - Add the “checking” function below to your
~/.bashrc
. (Sorry, fish/zsh/nushell/powershell users. I’m sure you’re smart enough to convert this stupid-simple function to your shell scripting language of choice!) - Run
source ~/.bashrc
. - Optional: to avoid entering your password every time, edit your sudoers file (with
sudo visudo /etc/sudoers
) and add two lines to the end:user ALL=NOPASSWD: /usr/bin/ln -sf /etc/hosts-checking /etc/hosts
anduser ALL=NOPASSWD: /usr/bin/ln -sf /etc/hosts-doing /etc/hosts
(replacing "user" with your username).
Here’s how to use it:
checking 15m
You can pass the duration in any unit the sleep function accepts (checking 12s
, checking 1hr
, etc)
Here’s the thing itself:
function checking () {
#set -x
#set -o errexit
export DURATION="${1:-1m}"
echo "Starting a Checking session lasting ${DURATION}"
sudo ln -sf /etc/hosts-checking /etc/hosts && resolvectl flush-caches
echo "Distracting websites and comms unblocked!"
sleep "$DURATION"
notify-send "Checking session complete! Close your tabs."
sleep 1m
sudo ln -sf /etc/hosts-doing /etc/hosts && resolvectl flush-caches
echo "Done"
trap "sudo ln -sf /etc/hosts-doing /etc/hosts" INT # Handle Ctrl+C
#systemctl suspend
}
It’s so simple, it’s essentially self-documenting.
Notice the 1-minute grace period, with a reminder that it’s time to wrap things up.
The optional suspend at the end is just to drive the point home, and give a moment to clear the brain. (If I actually pay attention and stop when the notification pops up, I can hit Ctrl+C
.)
Here are some ideas of what you can block in your hosts-doing
:
- News sites
- Analytics and dashboards
- Feed readers
- Social media
- Email and Chat
127.0.0.1 news.ycombinator.com lobste.rs
127.0.0.1 plausible.io googleanalytics.com goatcounter.com umami.is usefathom.com artists.spotify.com
127.0.0.1 commafeed.com feedly.com inoreader.com
127.0.0.1 reddit.com facebook.com x.com instagram.com bsky.social threads.net craigslist.org
127.0.0.1 gmail.com slack.com discord.com
This is just an example. I only bother blocking services I (over) use, and I put one site per line for readability.
Obviously, don’t block anything you need for your real work.
Also, to borrow Tiny Tiny RSS’s charming disclaimer1: “There’s no warranty. If it breaks you get to keep both parts.”