22

I often find myself man'ing a command just to learn about one specific option. Most of the time I can search to the option just fine, unless it's something like ffmpeg or gcc where I have to step through about 40 matches until I get to the actual description of the option...

Sometimes I can get lucky and search for the word "options" to get close and then refine it from there, but it would be nice if I could reliably jump straight to the option in question. It would be cool if there was a tool that could parse out the options and build a database on which you could do searches, but after looking at the groff markup for a few pages I've determined it would only be a best-guess effort due to the lack of meta-information in groff markup... In my ideal world woman mode in emacs would support searching for specific options... :)

Any tips for jumping straight to a specific option in a man page?

4 Answers 4

13

Here's my script to do it. It's called he.

#!/bin/sh
# he - print brief help about a single option or command
# Mikel Ward <[email protected]>

# Example Usage:
# he bash continue
# he rsync -v

scriptname=he

usage()
{
    cat <<EOF 1>&2
Usage: $scriptname <command> [<option|section>]
Example:
    $scriptname bash getopts (shows documentation for bash getopts)
    $scriptname ssh -v       (shows documentation for ssh -v flag)
    $scriptname select       (shows SYNOPSIS for select(2))
    $scriptname 'open(2)'    (shows SYNOPSIS for open(2))
EOF
}

if test $# -lt 1; then
    usage
    exit 2
fi

manpage="$1"
# show the SYNOPSIS section if no section or option was given
option="${2:-SYNOPSIS}"

# handle manpage(number)
case $manpage in *\(*\))
    page=${manpage%\(*\)}
    section=${manpage#$page}
    section=${section#\(}
    section=${section%\)}
    manpage=$page
    ;;
esac

man ${section:+-s $section} "$manpage" | perl -n -e '
BEGIN {
    $option = "'$option'";
    $inside_option = 0;
}
if (!$inside_option) {
    if (/^(\s*)\Q$option\E\b/p) {
        # start of this option
        $option_indentation = $1;
        $inside_option = 1;
        $saw_blank_line = 0;
        print;
    }
} else {
    if (/^$/) {
        $saw_blank_line = 1;
        print;
    } elsif (/^\Q$option_indentation\E\S/ and $saw_blank_line) {
        # item at same indentation => start of next option
        $inside_option = 0;
    } elsif (/^\S/) {
        # new heading => start of next section
        $inside_option = 0;
    } else {
        print;
    }
}
'
$ he cp    
SYNOPSIS
       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... SOURCE... DIRECTORY
       cp [OPTION]... -t DIRECTORY SOURCE...

$ he gcc -dD
       -dD Dump all macro definitions, at the end of preprocessing, in addition to normal output.

$ he rsync -v
        -v, --verbose               increase verbosity

$ he bash getopts
       getopts optstring name [args]
              getopts is used by shell procedures to parse positional parameters.  optstring contains the option characters to be recognized; if a character is  followed  by  a  colon,  the  option  is
              expected  to  have  an  argument, which should be separated from it by white space.  The colon and question mark characters may not be used as option characters.  Each time it is invoked,
              getopts places the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variable OPTIND.  OPTIND  is
              initialized  to  1 each time the shell or a shell script is invoked.  When an option requires an argument, getopts places that argument into the variable OPTARG.  The shell does not reset
              OPTIND automatically; it must be manually reset between multiple calls to getopts within the same shell invocation if a new set of parameters is to be used.

              When the end of options is encountered, getopts exits with a return value greater than zero.  OPTIND is set to the index of the first non-option argument, and name is set to ?.

              getopts normally parses the positional parameters, but if more arguments are given in args, getopts parses those instead.

              getopts can report errors in two ways.  If the first character of optstring is a colon, silent error reporting is used.  In normal operation diagnostic messages are printed  when  invalid
              options or missing option arguments are encountered.  If the variable OPTERR is set to 0, no error messages will be displayed, even if the first character of optstring is not a colon.

              If  an  invalid  option  is  seen, getopts places ? into name and, if not silent, prints an error message and unsets OPTARG.  If getopts is silent, the option character found is placed in
              OPTARG and no diagnostic message is printed.

              If a required argument is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a diagnostic message is printed.  If getopts is silent, then  a
              colon (:) is placed in name and OPTARG is set to the option character found.

              getopts returns true if an option, specified or unspecified, is found.  It returns false if the end of options is encountered or an error occurs.

But if you don't have access to a script like that, just run less, then type /^ *-option (note the space), for example, in the gcc man page, type /^ *-dDEnter to find the documentation for the -dD option.

This works because the option usually appears at the start of the line.

5
  • 1
    Imagine a big bearded bear man kissing your toes for this!
    – sepehr
    Commented Nov 2, 2014 at 5:46
  • Ha ha! Thanks! Also note that I renamed the script to he, as in "short help". The latest version is on github
    – Mikel
    Commented Nov 2, 2014 at 14:52
  • New link: github.com/mikelward/scripts/blob/main/he
    – Mikel
    Commented Jun 22, 2020 at 6:42
  • 1
    /^ *-option\> is what I use. Matching the end of the word is useful for tools that have many options with the same prefix.
    – pavon
    Commented Aug 11, 2023 at 20:14
  • Adding col -b to the man pipe made it work for me on macOS. See here for details. Commented Nov 13, 2023 at 19:10
5

Easiest way to jump to a specific option if you want to look up the 1 letter code: type 3 spaces then the option using the "/" man search feature.

For example, let's say we want to jump to the -p option of the grep manual.

Search string:

/   -p

Match

 -p      If -R is specified, no symbolic links are followed.  This is the
         default.
2
  • 2
    Simple and pragmatic. Like!
    – mgois
    Commented Nov 17, 2023 at 18:09
  • My favorite answer. Straight to the point! Commented Apr 8 at 16:59
3

This is the function I use. I call it "mans" for "man search".

mans ()
{
    local pages string;
    if [[ -n $2 ]]; then
        pages=(${@:2});
        string="$1";
    else
        pages=$1;
    fi;
    man ${2:+--pager="less -p \"$string\" -G"} ${pages[@]}
}

Usage:

$ mans ^OPTIONS grep find xargs
2
  • Sweet! Not exactly the "ideal" lookup-table type solution I was hoping for, but still very useful. Thanks.
    – mgalgs
    Commented Mar 4, 2011 at 7:24
  • 1
    As noted below, most of the time what you're looking for is at the start of the line after some indentation, so the pattern will usually look like mans '^ *<something>' <page>. See my answer for more details.
    – Mikel
    Commented Mar 4, 2011 at 8:32
1

This is the function I use to display the option description and command description.

#!/usr/bin/env bash

usage() {
    cat <<EOF
Name: manop
Description: This script outputs a command description or an option 
description from the man page.
Usage:  
    manop wget -b : Show wget -b option description
    manop ls: Show the ls command description
EOF
    exit 2
}

if [ $# -eq 0 ]; then
    usage
fi

if [ $# -eq 2 ]; then
    man "$1" | col -bx | sed -n "/^  *$2/,/^$/p"
else
    man "$1" | col -bx | sed -n "/^DESCRIPTION/,/^$/p"
fi

exit 0

How to use it:

$ manop ls -h
       -h, --human-readable
              with -l and -s, print sizes like 1K 234M 2G etc.
$  manop col
DESCRIPTION
     The col utility filters out reverse (and half reverse) line feeds so that 
     the output is in the correct order with only forward and half forward line feeds, 
     and replaces white-space characters with tabs where possible. This can be useful 
     in processing the output of nroff(1) and tbl(1).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .