Skip to content

Instantly share code, notes, and snippets.

@ernstki
Created November 2, 2022 09:12
Show Gist options
  • Save ernstki/fb49d948ee9cb7676764ce074090c431 to your computer and use it in GitHub Desktop.
Save ernstki/fb49d948ee9cb7676764ce074090c431 to your computer and use it in GitHub Desktop.
Fetch man pages from manpages.debian.org and typeset in the terminal (for macOS)
#!/usr/bin/env bash
##
## lman - fetch GNU/Linux man pages from manpages.debian.org
##
## Author: Kevin Ernst
## Date: 1 November 2022
## License: Unlicense - https://unlicense.org/#the-unlicense
##
# can override any of these by setting LMAN_* in the environment
MAXWIDTH=${LMAN_MAXWIDTH:-100}
ENCODING=${LMAN_ENCODING:-utf8}
LANG=${LMAN_LANG:-en}
BASEURL=${LMAN_BASEURL:-'https://manpages.debian.org/stable'}
_lman() {
local termwidth=$(tput cols)
local width=$(( termwidth < MAXWIDTH ? termwidth : maxwidth ))
local margin=$(( (termwidth - width) / 2 ))
local pad=$(printf "%${margin}s")
local manpage=${1:?What manual page do you want?}
# center MAXWIDTH columns within the terminal
curl -sL "$BASEURL/$manpage.$LANG.gz" \
| groff -man -T$ENCODING -rLL=${width}n \
| sed "s/^/$pad/" \
| less
}
# run the function if the script is being executed
[[ $0 == $BASH_SOURCE ]] && _lman "$@"
@ernstki
Copy link
Author

ernstki commented Nov 2, 2022

Why would you ever want to do this? Maybe you're a sysadmin, writing scripts that need to run on both macOS/BSD and GNU/Linux.

It's useful when you want to know what are the lowest common denominator options supported by, say, find or grep on Mac/BSD and Linux.

See also

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