chrispooledotcom

Quick Man Page Lookup

24 October 2011

I often find myself wanting to look up an option in a unix man page. This is a little function I wrote to save myself some typing. It’ll also successfully find text that’s written in bold font in the manual, which simple grepping will not do.

Put it in your .bashrc or .zshrc file. I’ve tested it with zsh on Mac OS X Snow Leopard.

gman () {
    3=${3:-'0'}
    man -- $1 | col -bx | if [[ $2 == "" ]]; then
        grep -Em2 -- "^ +$1"
    else
        if [[ $3 -lt 0 ]]; then
            grep -C${3#-} -- $2
        else
            grep -A$3 -- $2
        fi | more
    fi
}

Invoke it like this:

gman (program name) [search term] [[-]num]

Example usage

gman grep
Prints rough usage of the command grep

grep, egrep, fgrep - print lines matching a pattern grep [options] PATTERN [FILE…]

gman grep -o
Prints lines matching -o in grep’s man page, usually explaining how to use it
There’s no need to add -- before the search term
gman grep -o 3
Same as above, showing the next 3 lines after each matching line
gman grep -o -3
Show 3 lines around each matching line
gman bash '!!' -20
Quickly check the event designators section of the bash manual

The output is passed to more, the pager program, so longer results won’t simply disappear off your screen.