Skip to content

Instantly share code, notes, and snippets.

@uliska
Last active September 13, 2017 06:48
Show Gist options
  • Save uliska/91ab82c5a4f6b0fe258d35d8f8f54b5e to your computer and use it in GitHub Desktop.
Save uliska/91ab82c5a4f6b0fe258d35d8f8f54b5e to your computer and use it in GitHub Desktop.
View manpages as PDF and optionally create them before
#!/bin/bash
# convert a manpage to PDF
echo
echo "view-man"
echo "Open a manpage as PDF"
echo
if (( $# == 0 ))
then
echo
echo "Usage: view-man <page-name>"
echo "The single argument is the name of the manpage."
echo "If no manpage with that name exists the program stops."
echo "If a file ~/.man2pdf exists it should contain a single absolute path"
echo "to a directory containing converted PDF files of manpages (without trailing slash)."
echo "Otherwise the program works in the current directory."
echo
echo "If a PDF for the given manpage exists it is opened, otherwise"
echo "it is first created."
exit 1
fi
pg=$1
# Check if a manpage exists for the given argument
if man "$pg" > /dev/null 2>&1
then
# Use common directory or current working dir
if [ -e ~/.view-man ]
then
targetdir=$(cat ~/.view-man)
if ! [ -d $targetdir ]
then
echo "Content of ~/.view-man doesn't point to an existing directory! Exiting"
exit 1
fi
else
targetdir=$(pwd)
fi
out="$targetdir/$pg.pdf"
if ! [ -e $out ]
then
echo "Create PDF from manpage for $pg"
man -t $pg | ps2pdf - $out
fi
xdg-open $out
else
echo "No manpage for $pg found"
fi
@uliska
Copy link
Author

uliska commented Sep 13, 2017

This is a convenience utility around the possibility to convert manpages to PDF, it is inspired by TeX Live's texdoc' utility.

Usage:
view-man <program-name>, e.g. view-man pstree.

The script works either in the current directory or in a common folder where manpage PDFs are maintained.
If a file ~/.view-man is found its content is interpreted as an absolute path to the common folder.

If the argument names an existing manpage the script looks for the corresponding PDF in the target directory. If one is present it is simply opened with the user's PDF viewing application, otherwise it is first created and then opened.

Preparation:
Save the file, ideally to a directory in $PATH (e.g. ~/bin'') and make it executable. Create a directory to hold the generated PDF manpages. Create a file ~/.view-man` with the absolute path to the created directory as its only content.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment