Retrieve formatted manual page from command line [duplicate]

I try to export the manual page of grep to grep.txt

    $ man grep > ~/desktop/grep.txt

It displayed in garbled code:

            GREP(1)                   BSD General Commands Manual                  GREP(1)

            NNAAMMEE
                 ggrreepp, eeggrreepp, ffggrreepp, zzggrreepp, zzeeggrreepp, zzffggrreepp -- file pattern searcher

            SSYYNNOOPPSSIISS
                 ggrreepp [--aabbccddDDEEFFGGHHhhIIiiJJLLllmmnnOOooppqqRRSSssUUVVvvwwxxZZ] [--AA _n_u_m] [--BB _n_u_m] [--CC[_n_u_m]]

How to get a formatted manual page from command line?


You can use the following example command to get formatted plain text output for a manual page:

man grep | col -bx > ~/Desktop/grep.txt

Produces, e.g.:

GREP(1)                   BSD General Commands Manual                  GREP(1)

NAME
     grep, egrep, fgrep, zgrep, zegrep, zfgrep -- file pattern searcher

SYNOPSIS
     grep [-abcdDEFGHhIiJLlmnOopqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
          [-e pattern] [-f file] [--binary-files=value] [--color[=when]]
          [--colour[=when]] [--context[=num]] [--label] [--line-buffered]
          [--null] [pattern] [file ...]

To automate the use of e.g. man grep | col -bx > ~/Desktop/grep.txt command in Terminal, so you can just type e.g. mant grep, or other command, it can be wrapped in a bash function placed in your .bash_profile file.

I normally prefer to have my manual pages in PDF format however I've modified the manp bash function I use in my .bash_profile file to work for plain text as, mant.

For plain text manual pages:

function mant () 
{ 
    mpdir="$HOME/Documents/Manual Pages"
    [[ ! -d $mpdir ]] && mkdir -p "$mpdir"
    if [[ ! -z $1 ]]; then
        if [[ ! -f $mpdir/$1.txt ]]; then
            man "$1" | col -bx > "$mpdir/$1.txt"
            open -e "$mpdir/$1.txt"
        else
            open -e "$mpdir/$1.txt"
        fi
    else
        printf '  Error: Missing argument!...\n  Syntax: mant command_name\n  Example: mant man\n'
    fi
}

For PDF manual pages:

function manp () 
{ 
    mpdir="$HOME/Documents/Manual Pages"
    [[ ! -d $mpdir ]] && mkdir -p "$mpdir"
    if [[ ! -z $1 ]]; then
        if [[ ! -f $mpdir/$1.pdf ]]; then
            man -t "$1" | pstopdf -i -o "$mpdir/$1.pdf"
            open "$mpdir/$1.pdf"
        else
            open "$mpdir/$1.pdf"
        fi
    else
        printf '  Error: Missing argument!...\n  Syntax: manp command_name\n  Example: manp man\n'
    fi
}

To add either or both bash functions shown above to your .bash_profile file, in Terminal use the following command:

nano "$HOME/.bash_profile"

Then copy and paste the function(s) into the .bash_profile file, then save the file and exit nano pressing the following keys:

  • Control+X
  • Y
  • Enter

Close and reopen the Terminal app, not just the window, and you are ready to go.

For the default manual page in Terminal, e.g.:

man grep

For a plain text file manual page, e.g.:

mant grep

For a PDF file manual page, e.g.:

manp grep

With the bash functions, if a file for the given command doesn't already exist, it's created and then opened. If the file of the command already exists, it's just opened. This way you can easily access the manual page in the form you'd like while in Terminal.

If you want to place the files in a different location, then change the path portion of the following line of code as appropriate:

mpdir="$HOME/Documents/Manual Pages"

The rest of the code within is tokenized to use $mpdir for the location of the files.


As a side note, if you just want the complete manual page for a given command, in a Terminal window, which is easily fully scrollable, you can do the following...

Just type the command name by itself, then right-click on it and select: Open man Page

This is better then typing e.g. man grep and pressing enter, because it's fully scrollable and you do not need to navigate using key sequences as defined in the less command utility used as the pager by man.