---
title: w3m-reddit
layout: post
redirect_from: "/blog/w3m-reddit/"
---

I've been moving a lot of my daily tasks to the command-line lately, and that
includes redditing. I probably spend far too much time on
[reddit](http://reddit.com) as it is, but I really wanted to find an efficient
way to view reddit through the command-line.  [w3m](http://w3m.sourceforge.net/)
could render reddit okay, but I couldn't view my personal front-page because
that required me to login to my profile.

The solution was [cortex](http://cortex.glacicle.org/), a CLI app for viewing
reddit.

However, I kind of got tired of viewing reddit through w3m, the header alone is
a few pages long to scroll through, and the CSS for the comments doesn't load so
there isn't any sense of threading. But, then I discovered reddit's mobile
website: [http://reddit.com/.mobile](http://reddit.com/.mobile), and it looks absolutely
beautiful in w3m. In fact, I think I prefer it to the normal website in any
modern browser; there are no distractions, just pure content.

<a href="/img/blog/w3m_mobile_reddit.png"><img src="/img/blog/w3m_mobile_reddit.png" alt="m.reddit.com rendered in w3m"></a>

In order to get cortex to open the mobile version of reddit, I made a bash
script wrapper around w3m that takes urls and appends `".mobile"` to the end of
reddit urls before passing them to w3m (as well as fixing a double forward slash
error in the comment uri cortex outputs that desktop reddit accepts but mobile
reddit 404s on). The script:

~~~ bash
#!/bin/bash

args=()
until [ -z "$1" ]; do
  case "$1" in
    -t|--tmux) t=1; shift ;;
    --) shift ; break ;;
    -*) echo "invalid option $1" 1>&2 ; shift ;; # or, error and exit 1 just like getopt does
    *) args+=("$1") ; shift ;;
  esac
done

args+=("$@")
for arg in "${args[@]}" ; do
    # Switch to mobile reddit
    url=$arg
    mobile='.mobile'
    if [[ $url =~ http:\/\/www.reddit.com || $url =~ http:\/\/reddit.com ]]
    then
        if [[ $url =~ \/$ ]]
        then
            url=$url$mobile
        else
            url=$url'/'$mobile
        fi
    fi
    # Fix double backslash error in comment uri for mobile reddit
    url=${url/\/\/comments/\/comments}
    if [[ $t == "1" ]]; then
        tmux new-window 'w3m "'${url}'"'
    else
        w3m "${url}"
    fi
done
~~~

Since I regurally use [Tmux](http://tmux.sourceforge.net/) (with
[Byobu](http://byobu.co/)), I also added an optional `-t`/`--tmux` switch that
will open w3m in a temporary new tmux window that will close when w3m is closed.

I saved the script as `w3m-reddit` and made it an executable command. In Ubuntu
that's done with the following commands:

~~~ bash
$ sudo mv w3m-reddit /usr/bin/
$ sudo chmod +x /usr/bin/w3m-reddit
~~~

Now cortex needs to be configured to use `w3m-reddit`, and that's done by
setting `browser-command` in the cortex config at `~/.cortex/config` to
`w3m-reddit`:

    ## Command to invoke the webbrowser
    ## If left empty will try to autodetect the system default browser
    ##browser-command=firefox '{0}'
    browser-command=w3m-reddit '{0}'

The result is a distraction-free reddit experience right in the command-line
without having to edit cortex directly. I've found that I even prefer reddit
this way. Without image thumbnails (I need to explicitly select the image links
to view the image in w3m) I am more inclined to pay equal attention to every
post, not just mindlessly scrolling through meme-fests. Thus I'm more focused
and tend to not loose myself like I do in the infinite scrolling of [RES
reddit](http://redditenhancementsuite.com/) in a GUI browser.

There are still some improvements I could make to the w3m-reddit script. Namely,
it should pass along any arguments to itself to w3m underneath. I'm still a
newby at bash though, and I couldn't figure out an easy way to do that without
scrapping the whole thing and starting over in Python instead.

Stay tuned for more posts on how I view images and videos efficiently from the
command-line.

EDIT 04/25/2015: Reddit seems to have gotten rid of their old mobile reddit site
and replaced it with a more modern version that unfortunately doesn't look as
good in w3m. However, the old mobile site is still accessable by adding a
".mobile" to the end of urls. The script above has been edited to reflect this
change.
