Getting 'man' pages into Preview easily and safely

I was recently reading Macworld and saw a tip for easily opening ‘man’ pages in Preview.

The tip isn’t half bad but suffers from the typical problems that Mac gurus run into when playing around with the Unix underpinnings of OS X. They mistake simple for simplicity and elegance.

Let’s take a look at Rob’s solution and see about making it clean and neat.

First, Rob suggests creating the solution as a shell function and placing it in your .profile. Sure, for the simple code he has this isn’t a bad idea, however if, like me, you already have a personal ‘bin’ folder in your path then a tiny shell script would do better.

Second, if you have a look at the comments to Rob’s articles you’ll see that others have pointed out the two problems that leapt to my attention – what about passing multiple arguments to ‘man’ and what about checking that we will actually get valid output. When I was reading it nobody had put all three together so let’s do it.


!#/usr/bin/env bash
if [ "${1}" ]; then
if man -w $@ >/dev/null; then man -t $@ | open -f -a Preview; fi
else
echo "Usage: pman [-n] command" where n is section number and command is a Unix command
fi

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s