Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.3: Easily view man pages with Preview UNIX
This is a Panther-specific update to an older hint regarding viewing man pages with Preview. Now that Preview supports viewing PostScript files and also includes the necessary PostScript utilities you can simply do something along the lines of:

#!/bin/sh
man -t $1 > /tmp/$1.ps
open /tmp/$1.ps
This will create a PostScript file for the command you're trying to get the man page of, store it in the /tmp directory, and then open it using Preview.

[robg adds: Though this is technically an update to an older hint, I'm running it as new because (a) it's much simpler than the previous solution, as ghostscript no longer needs to be installed, and (b) because it's so nice. I created the script and called it preman and made it executable (chmod 755 preman). Now it's a joy to browse and read huge things like the tcsh man page. There are, of course, third-party apps like ManOpen and manThor that do the same thing, if you prefer a pure GUI solution.]
    •    
  • Currently 1.71 / 5
  You rated: 3 / 5 (7 votes cast)
 
[15,846 views]  

10.3: Easily view man pages with Preview | 36 comments | Create New Account
Click here to return to the '10.3: Easily view man pages with Preview' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3: Easily view man pages with Preview
Authored by: defpo on Nov 02, '03 12:55:06PM

Do I save this in a file? Put it where? Add it to a file (like .bashrc)?



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: dxterra on Nov 02, '03 01:46:49PM

I did what rob said. In my home directory, I created a file called preman. I placed the code above in the file and changed it to be executable (chmod 755 preman). Then I call it like this from my home directory:
./preman bash

I guess you could place an alias in your .bash_profile if you are using Panther. Like this:
alias preman='$HOME/preman'

That way no matter where you are, you can call it.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: liquidgood on Nov 02, '03 01:47:58PM
this is a great hint!

you can paste that code in a text file, chmod 755 it and put it somewhere in your $PATH, like ~/bin.
or if you use zsh, add this to your functions file:

function manps { man -t "$1" > /tmp/"$1".ps; open /tmp/"$1".ps; }

this works exactly the same way, but it gets loaded when you login to the terminal.

[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: defpo on Nov 02, '03 01:56:03PM

Thanks guys. I thought what rob said was for something else. Up and running!



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: sapporo on Nov 02, '03 02:44:06PM
If you regularly view large manpages, you might want to add caching, too. This function creates a pdf if it isn't there already and opens it (that's one single line of course):

function manpdf { if [[ ! -r /tmp/"$1".pdf ]]; then man -t "$1" | pstopdf -i -o /tmp/"$1".pdf; fi; open /tmp/"$1".pdf; }

I guess it could use some error handling to let it fail gracefully in case no man page is found, but I'll leave that up to you.

[ Reply to This | # ]

10.3: Easily view man pages with Preview
Authored by: dxterra on Nov 02, '03 01:36:38PM

This is awesome! I always hated viewing man pages. This makes it so much easer.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: milksheikh on Nov 02, '03 02:36:54PM
Nice tip!

At least in tcsh, you can avoid needing to create a separate script file by adding an alias to your .cshrc file:


alias preman 'man -t \!^ > /tmp/\!^.ps; open /tmp/\!^.ps'
No idea if this works under bash...

I'm also in the habit of putting aliases in the file ~/.aliases, and then reading that in using

source ~/.aliases
in my .cshrc.

That way, I can add a new alias quickly with "cat >> ~/.aliases" and reload by typing "source ~/.aliases", and also keeping all the aliases together nice and neatly.

Important aliases for the command line hacker:


alias rm 'rm -i'
alias cp 'cp -i'
alias mv 'mv -i'
Reduces the risk of you deleting/overwriting anything inadvertantly.

alias ls 'ls -l'
is also a must for me.

[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: mistercow on Nov 02, '03 02:46:09PM
Great hint! I would recommend using the new "pstopdf" to convert it to pdf before opening it. It seems to convert faster from the command line than in preview. The slightly modified script looks like this:

#!/bin/sh
man -t $1 > /tmp/$1.ps
pstopdf /tmp/$1.ps /tmp/$1.pdf
open /tmp/$1.pdf


[ Reply to This | # ]
Deleting the file
Authored by: triplef on Nov 02, '03 03:10:48PM
If you don't want to rely on the system to remove files in /tmp, you can change the script like this to automatically remove the file 60 seconds after it has been opened. This version will to the PS to PDF conversion using pstopdf, which is - as noted above - a bit faster than Preview.
man -t $1 | pstopdf -i -o /tmp/$1.pdf
open /tmp/$1.pdf
sleep 60 && rm -f /tmp/$1.ps &


[ Reply to This | # ]
Deleting the file
Authored by: pualo2 on Nov 02, '03 04:58:50PM
Might want to change that. You are saving a .pdf file but trying to delete a .ps file. Change the ".ps" in the last line to ".pdf" so you get:

man -t $1 | pstopdf -i -o /tmp/$1.pdf
open /tmp/$1.pdf
sleep 60 && rm -f /tmp/$1.pdf &


[ Reply to This | # ]
debugging fonts
Authored by: atl on Nov 02, '03 03:32:07PM
Great hint. I had a tough time getting it to work because of some apparently corrupted fonts on my system. Running pstopdf from the command line helped see which fonts were unusable. (The font listing would stall at the offending font.) Interestingly, Font Book.app's Disable settings weren't respected by pstopdf, so I had to physically move the offending fonts from the font folder. Once pstopdf could scan all my fonts properly, this hint worked like a champ!

[ Reply to This | # ]
Improved Script
Authored by: romano_a on Nov 02, '03 03:58:16PM
You should replace the mentioned folders in the script with relevant folders for you

The Script


#!/bin/sh
man -t $1 > /tmp/$1_man.ps
pstopdf /tmp/$1_man.ps -o "$HOME/Documents/UNIX/Man Pages/$1.pdf"
rm /tmp/$1_man.ps
open "$HOME/Documents/UNIX/Man Pages/$1.pdf"



[ Reply to This | # ]
A zsh function
Authored by: sabi on Nov 02, '03 04:37:09PM

  gman() {
    PDF=/tmp/man.$$.pdf
    print 'Converting to PDF...'
    man -t $@ | pstopdf -i -o $PDF
    print 'Opening in Preview...'
    launch -c prvw $PDF
    { sleep 5; rm -f $PDF } &!
  }
Feel free to dump the 'print' statements if you don't like progress. Personally, the first one I tried was 'zshall', which takes quite a while to generate.

[ Reply to This | # ]
For the shell-scripting challenged...
Authored by: porkchop_d_clown on Nov 02, '03 04:39:31PM

We should point out that this operation will leave postscript files lying around in the /tmp folder.

Here's a version that cleans up after itself:

#!/bin/tcsh

man -t $1 >/tmp/$1.ps
open /tmp/$1.ps
( sleep 10; rm /tmp/$1.ps) &

---
Everyone loves a clown, but no one will lend him money!

[ Reply to This | # ]

For the shell-scripting challenged...
Authored by: romano_a on Nov 02, '03 04:47:41PM
Worng

The OS remove all files in the /tmp folder on every reboot.

[ Reply to This | # ]

For the shell-scripting challenged...
Authored by: Anidel on Nov 02, '03 06:14:26PM
What about this script:

function pman ()
{
        if [ ! $1 ]; then
                echo "What manual page do you want!?"
                exit ;
        fi

        if [ ! $2 ]; then
                MAN_PAGE=$1;
        else
                MAN_PAGE=$2;
                MAN_SECTION=$1;
        fi;

        CACHE_PATH="$HOME/Documents/ManCache"
        CACHE_FILENAME=$CACHE_PATH/man.$MAN_SECTION.$MAN_PAGE.pdf

        if [ ! -e "$CACHE_PATH" ]; then
                mkdir -p $CACHE_PATH;
        fi

        if [ ! -e "$CACHE_FILENAME" ]; then
                man $@ && (man -t $@ | pstopdf -p -i -o $CACHE_FILENAME; open $CACHE_FILENAME; )
        fi
}
it'll keep a cache of already created man pages. It also checks if the man page requested does exists or not. Well, it's not complete, but it's a bit more useful of the original hint.

You can put it in your $HOME/.bash_profile or $HOME/.bashrc

---
Anidel

[ Reply to This | # ]

For the shell-scripting challenged...
Authored by: aranor on Nov 02, '03 06:39:05PM
Here's a shell script based on yours, but actually does everything. It checks to make sure the manpage exists, it opens the page if it's already been cached (I didn't try yours but I don't see where yours would do that), and it uses /tmp/ManCache/ as the cache directory. It's also done in shell script format instead of function format. Here it is:
#!/bin/sh
if [ ! $1 ]; then
	echo "What man page do you want?";
	exit;
fi;

if [ ! $2 ]; then
	MAN_PAGE=$1;
	MAN_SECTION="1";
else
	MAN_PAGE=$2;
	MAN_SECTION=$1;
fi;

CACHE_PATH="/tmp/ManCache"
CACHE_FILENAME=$CACHE_PATH/$MAN_PAGE.$MAN_SECTION.pdf

if [[ ! -d "$CACHE_PATH" ]]; then
	mkdir -p $CACHE_PATH;
fi;

if [[ -r $CACHE_FILENAME ]]; then
	open $CACHE_FILENAME;
elif [[ `man -w "$@"` != "" ]]; then
	man -t $@ | pstopdf -i -o $CACHE_FILENAME; open $CACHE_FILENAME;
fi;


[ Reply to This | # ]
For the shell-scripting challenged...
Authored by: superfly on Nov 13, '03 09:36:14AM
this version gets the section right if it's not given.

#!/bin/sh
CACHE=/tmp/preman

if [ ! $1 ]; then
    # give man's wtf error
    man
    exit
fi

# get manpage name
MANPAGE=`man -w $@`
if [ ! $? = 0 ]; then  # not found
    # man will have printed an error
    exit
fi

MANPAGE=`basename $MANPAGE`
PDF=$CACHE/$MANPAGE.pdf

# create cache if it doesn't exist
if [[ ! -d $CACHE ]]; then
    mkdir -p $CACHE
fi

# if there's no cached pdf, make one
if [[ ! -r $PDF ]]; then
    man -t $@ | pstopdf -i -o $PDF
fi

open $PDF


[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: Drakonian on Nov 02, '03 04:57:05PM

Good hint - one problem is that I find .ps files open in Adobe Acrobat Reader 6 for me by default. (Not sure if I set this setting). Anyone know how to get them to open in Preview, by default? I'm getting around it but converting to PDF on the command line first which seems faster anyway, but just for future reference?



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: defpo on Nov 02, '03 06:57:25PM

Right/Control click on a pdf file, and select "get info." Change the "open with" application to Preview and then hit the "change all" button. That should do it.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: DanPritchard on Nov 02, '03 10:24:47PM

Or in this case, "right-click on a PS file" (not a PDF).

Feel free to "manufacture" a "PS" file just by temporarily renaming any file to that, to get this job done, if you don't have any real PS files handy.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: Drakonian on Nov 03, '03 12:53:21AM

Great. Thanks guys. (Slight newbie to OS X here, but I'm loving it.)



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: adrianm on Nov 08, '03 09:11:48AM
to open a .ps file in preview regardless of what your defaults are set to, try this:
open -a 'preview' file.ps
of course this works for any named application and file, eg:
open -a 'subethaedit' /etc/hostconfig
and so on...

[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: Anidel on Nov 02, '03 06:23:56PM
Sorry, my first script was totally wrong :) This one is better, but does not test wether a man page exists or not. With a bit of more work it can do a better work in checking parameters as well as non-existence of man pages.

function pman ()
{
        if [ ! $1 ]; then
                echo "What manual page do you want!?"
                exit ;
        fi

        if [ ! $2 ]; then
                MAN_PAGE=$1;
        else
                MAN_PAGE=$2;
                MAN_SECTION=$1;
        fi;

        CACHE_PATH="$HOME/Documents/ManCache"
        CACHE_FILENAME=$CACHE_PATH/man.$MAN_SECTION.$MAN_PAGE.pdf

        if [ ! -e "$CACHE_PATH" ]; then
                mkdir -p $CACHE_PATH;
        fi

        if [ ! -e "$CACHE_FILENAME" ]; then
                man -t $@ | pstopdf -p -i -o $CACHE_FILENAME;
        fi

        open $CACHE_FILENAME;
}

Feel free to add it to your $HOME/.bash_profile or $HOME/.tcsh_login (if I remember well) scripts.

---
Anidel

[ Reply to This | # ]

10.3: Easily view man pages with Preview
Authored by: aranor on Nov 02, '03 06:40:05PM

See my reply to your previous script.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: Jarin on Nov 02, '03 06:41:35PM

For those of us with the Panther developer tools installed, an even nicer way of viewing man pages is to open Xcode, and select "Open man page..." from the Help menu. In addition to formatting the page, Xcode renders links into it, so you can jump to other sections or man pages. This feature is nice, but it's not hidden, so I'm surprised I haven't heard mention of it yet.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: aranor on Nov 02, '03 07:54:58PM

I just looked through the AS dictionary and I can't find any way of showing a man page using an AppleScript. Even if you try open file manpage it will just open the manpage file as a text file rather than a manpage.

Anybody see an AS command that I missed that would let you open a manpage from AS?



[ Reply to This | # ]
The perfect name for this script:
Authored by: DanPritchard on Nov 02, '03 10:07:07PM
I saved the script as:
/usr/bin/woman
/me goes off to laugh

[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: defpo on Nov 03, '03 03:49:48AM

The pdf(s) created in /tmp are not deleating. I'm using this variety:

#!/bin/bash

#man -t $1 > /tmp/$1.ps
#open /tmp/$1.ps

man -t $1 | pstopdf -i -o /tmp/$1.pdf
open /tmp/$1.pdf
sleep 60 && rm -f /tmp/$1.ps &

I've tried taking the number down to one, but they still seem to stick around. Any ideas?



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: timkingman on Nov 10, '03 04:49:22PM

The last line should rm /tmp/$1.pdf, not $1.ps



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: bluehz on Nov 03, '03 08:51:32AM

I don't really understand the big hoopla over this archaic method of viewing manpages. I mean - why go through the whole process of converting a man page to a ps file then viewing it. Seems like a lot of legwork. I personally think ManOpen is one of the more elegant solutions. Terminal friendly so you can simply enter openman manpage_name and it opens right up. very fast, low overhead. On top of that you get incredible search capabilities.



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: pjrgois on Mar 13, '04 09:41:08PM

Here is one that checks if there is a man page.

#!/bin/sh
#
# /Users/sandokan/.bin/pman
#
# This script will save man pages in PDF format to
# ~/Documents/man_pages and open them in Preview.app.
#
if [ ! $1 ]; then
echo "Please specify a man page to preview"
exit ;
fi

MAN_CHECK=`grep -c -w "$1(" /usr/share/man/whatis.db`

if [ "$MAN_CHECK" = "0" ]; then
echo "No manual entry for $1"
exit ;
fi

MAN_PATH="$HOME/Documents/man_pages"

if [ ! $2 ]; then
MAN_PAGE=$1;

if [ ! -e "$MAN_PATH" ]; then
mkdir -p "$MAN_PATH";
fi;

MAN_FILE=$MAN_PATH/$MAN_PAGE.pdf
else
MAN_PAGE=$2;
MAN_SECTION=$1;

if [ ! -e "$MAN_PATH/$MAN_SECTION" ]; then
mkdir -p "$MAN_PATH/$MAN_SECTION";
fi;

MAN_FILE=$MAN_PATH/$MAN_SECTION/$MAN_PAGE.pdf
fi;

if [ ! -e "$MAN_FILE" ]; then
man -t $@ | pstopdf -o "$MAN_FILE" -i;
open "$MAN_FILE";
else
open "$MAN_FILE";
fi



[ Reply to This | # ]
Revised version
Authored by: pjrgois on Mar 17, '04 07:55:39PM

#!/bin/sh
#
# /Users/sandokan/.bin/pman
#
# This script will save man pages in PDF format to
# ~/Documents/man_pages and open them in Preview.app.
#

echo ""
echo "::::Sript by Paulo Jorge Gois::::"
echo ""

if [ ! $1 ]; then
echo "Please specify a man page to preview"
exit 1;
fi

man -w $1 > /dev/null

if [ "$?" -ne 0 ]; then
exit 1;
fi

man_path="$HOME/Documents/man_pages"

if [ ! $2 ]; then
man_page=$1;

if [ ! -e "$man_path" ]; then
mkdir -p "$man_path";
fi;

man_file=$man_path/$man_page.pdf
else
man_page=$2;
man_section=$1;

if [ ! -e "$man_path/$man_section" ]; then
mkdir -p "$man_path/$man_section";
fi;

man_file=$man_path/$man_section/$man_page.pdf
fi;

if [ ! -e "$man_file" ]; then
man -t $@ | pstopdf -o "$man_file" -i;
open "$man_file";
else
open "$man_file";
fi



[ Reply to This | # ]
Revised version
Authored by: rodrilea on Dec 04, '04 11:31:22PM

I am new to shell scripting and I am running into problems with our script. When I run the script it displays the "Please specify a man page to preview." followed by a command prompt. If I enter the name of the man page it tries to run the application referenced by the man page name. Below is the code as I have entered it:

# /Users/rodrilea/.bin/pman
#
# This script will save man pages in PDF format to
# ~/Documents/man_pages and open them in Preview.app

echo ""
echo "::::Script by Paulo Jorge Gois::::"
echo ""
#
if [ ! $1 ]; then
echo "Please specify a man page to preview"
exit 1;
fi

man -w $1 > /dev/null

if [ "$?" -ne 0 ]; then
exit 1;
fi

MAN_PATH="$HOME/Documentes/man_pages"

if [! $2 ]; then
MAN_PAGE=$1;

if [ ! -e "$MAN_PATH" ]; then
mkdir -p "$MAN_PATH";
fi;

MAN_FILE=$MAN_PATH/$MAN_PAGE.pdf
else
MAN_PAGE=$2;
MAN_SECTION=$1;

if [ ! -e "$MAN_PATH/$MAN_SECTION" ]; then
mkdir -p "$MAN_PATH/$MAN_SECTION";
fi;

MAN_FILE=$MAN_PATH/$MAN_SECTION/$MAN_PAGE.pdf
fi;

if [ ! -e "$MAN_FILE" ]; then
man -t $@ | pstopdf-o "$MAN_FILE" -i;
open "$MAN_FILE";
else
open "$MAN_FILE";
fi

Thanks for your help.

Rod Rilea



[ Reply to This | # ]
10.3: Easily view man pages with Preview
Authored by: rodrilea on Dec 05, '04 01:14:23AM

I am new to shell scripting, and when I run your script I am finding that an empty directory called called Preview XXXX where the Xs are numbers is created in /tmp and a blank Textedit file called .ps is opened on the screen.

I have created a file called mantopdf with rights set to 755 and the file has your script as you have it listed. What am I missing or what am I doing wrong? Do I need to define $1 some place in the script?

I am running the script in OS 10.3.6.

Thanks for your help.

Rod Rilea



[ Reply to This | # ]
a simple working script
Authored by: eddybaby on Jan 07, '11 11:52:43AM

i couldn't get the script by Paulo Jorge Gois to work
and i wanted to save the results of the converted pdf only
so i wrote this simple script for dummies like me
it works and should be easy to understand and edit for dummies like me



#!/bin/sh
#
#
# Unix man page to PDF script "
# by eddybaby "
# with thanks to everyone "
#
# this script will convert any Unix man page to PDF format and open it
#
# the pdf is saved to your home folder in a folder named 'unix man pdf's'
#
# you can change the name or the path of the folder
# by editing this script in any text editor
#
# the script first creates a postscript file '.ps'
# then converts it to a pdf
# and then deletes the postscript file
# and then opens the pdf file
#
# how to use this script
#
# you must specify a man document to convert and open"
#
# open the script in a text editor [as you obviosly have done]"
#
# search for the string 'dummy_man_name' and "
# replace it with the the name of the man document you want to convert"
#
# ie - search for the string 'dummy_man_name' and"
# replace it with the string 'find' "
# to convert the unix manual for the find command to a pdf file"
#
#


echo ""
echo ""
echo " ====="
echo ""
echo " Unix man page to PDF script "
echo " by eddybaby "
echo " with thanks to everyone "
echo ""
echo " ====="
echo ""
echo " !!! if you see an error listed below !!!"
echo ""
echo " you did not specify a man document to convert and open"
echo ""
echo " open the script in a text editor"
echo ""
echo " and read the instructions"
echo ""
echo ""
echo " ====="
echo ""
echo ""

man -t dummy_man_name > ~/unix\ man\ pdf\'s/dummy_man_name_unix_command_manual.ps
pstopdf ~/unix\ man\ pdf\'s/dummy_man_name_unix_command_manual.ps ~/unix\ man\ pdf\'s/dummy_man_name_unix_command_manual.pdf
rm ~/unix\ man\ pdf\'s/dummy_man_name_unix_command_manual.ps
open ~/unix\ man\ pdf\'s/dummy_man_name_unix_command_manual.pdf



[ Reply to This | # ]