Skip to content

Instantly share code, notes, and snippets.

@b0o
Created March 10, 2022 06:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0o/cb223f703c0d6ec33ce9707b7392b384 to your computer and use it in GitHub Desktop.
Save b0o/cb223f703c0d6ec33ce9707b7392b384 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Use (neo)vim as your MANPAGER
# Set MANPAGER=/path/to/viman in your shellrc
#
# Copyright 2020-2022 Maddison Hellstrom <https://github.com/b0o>
# MIT License
set -Eeuo pipefail
shopt -s inherit_errexit
declare vim="nvim"
command -v nvim &>/dev/null || vim="vim"
if [[ ! -t /dev/stdin ]]; then
MANPAGER='less' man "$@" -l -
elif [[ $# -gt 0 ]]; then
args=()
manpages=()
cmd=""
opts_ok=1
while [[ $# -gt 0 ]]; do
[[ "$1" =~ ^- ]] && {
[[ $opts_ok -eq 0 ]] && {
echo "unexpected option $1" >&2
exit 1
}
args+=("$1")
shift
continue
}
opts_ok=0
manpage=""
if [[ "$1" =~ ^[[:digit:]]p?$ && $# -gt 1 ]]; then
manpage="$1 $2"
shift
elif [[ "$1" =~ ^([[:graph:]]*)\(([[:graph:]])\)$ ]]; then
manpage="${BASH_REMATCH[2]} ${BASH_REMATCH[1]}"
else
manpage="$1"
fi
manpages=("$manpage" "${manpages[@]}")
shift
done
[[ ${#manpages[@]} -eq 0 ]] && {
/bin/man "${args[@]}" || exit $?
exit 0
}
for i in $(seq 1 $((${#manpages[@]}))); do
p="${manpages[${#manpages[@]} - i]}"
[[ ${#cmd} -gt 0 ]] && cmd+=" | "
cmd+="M $p"
done
cmd+=" | tabfirst"
"${NVIM:-$vim}" "${args[@]}" -c "$cmd"
else
/bin/man
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment