Reading man pages on macOS

The other day I needed to look something up from man pages, diskutil and hdiutil. Because I wanted to cross-check some info AI gave me. More about that maybe in another post.

cpp

Usually, I used ss64’s Mac online man pages, but then I thought it might be nicer to have a PDF.

Especially search works better in PDF documents. The page preview in Preview is very helpful if the search term occurs a lot. Way better than going through an HTML page or searching in the terminal.

So I added a light tool, and since I know I will not back this up, but look for it when I need it on another machine, I document it here.

man2pdf

A quick tool to convert, and open, man pages to PDF documents is quickly done, especially in times of AI.

First, we need ps2pdf. This util is part of ghostscript, so let’s install that.

brew install ghostscript

Now it’s possible to make a simple man2pdf script.

#!/bin/bash

# man2pdf: Convert man page to PDF, optionally open it

if [ -z "$1" ]; then
    echo "Usage: man2pdf <manpage> [--open]"
    exit 1
fi

page="$1"
open_pdf=false

if [ "$2" == "--open" ]; then
    open_pdf=true
fi

if ! man "$page" >/dev/null 2>&1; then
    echo "Error: man page for '$page' not found."
    exit 1
fi

output="${page}.pdf"

man -t "$page" | ps2pdf - "$output" && echo "Created: $output"

if $open_pdf; then
    open "$output"
fi

Just set the executable bit:

chmod +x man2pdf

and ready is my man-page reader 🙂

./man2pdf hdiutil --open

Expanding the functionality

I will expand the script to use a common PDF location next time I have time.

Adding this functionality can be a nice exercise for interested users. Feel free to share your improvements in the comments.

If you know about any other useful tool for reading man pages, please share that in the comments.