Created
March 10, 2022 06:31
-
-
Save b0o/b5a816a624775e51bb16c6b748bc32bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
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