Skip to content

Instantly share code, notes, and snippets.

@treyharris
Created October 28, 2019 19:57
Show Gist options
  • Save treyharris/ebd5f96410c0c51e69dd45c5286ff506 to your computer and use it in GitHub Desktop.
Save treyharris/ebd5f96410c0c51e69dd45c5286ff506 to your computer and use it in GitHub Desktop.
# -*-shell-script-*-
# Often we want to search for a specific subsection in a manpage. This
# script does that by searching the manpage for a line that begins
# with the pattern (after accounting for whitespace).
#
# If PCRE support is compiled into less, we right-delimit the search
# pattern with the word-boundary anchor \b, since flag and subcomment
# sections often are bounded by other characters (like "-n=<num>").
# Otherwise we right-delimit with the space, equals sign, or left bracket.
#
# Depends on the lessHasPCRE function.
#
# FIX: Currently uses \b as anchor, but that doesn't work when the
# pattern itself ends with a non-word character, i.e. '\.'
[[ $#@ -eq 2 ]] || {
echoerr "Usage: $0 <manpage> <search pattern>"
return 1
}
local lesspat
local less
local manpager="${MANPAGER-${PAGER-/usr/bin/less}}"
if lessHasPCRE $manpager; then
lesspat='\s+'
lesspat+="$2"
lesspat+='\b'
else
lesspat="^ +$2[ =\(\{\[]"
fi
less="${LESS}-+i+/^${lesspat}"
if [[ -n "${ZSH_DEBUG}" && "${ZSH_DEBUG}" -ne 0 ]]; then
echo "running 'man $1' with 'LESS=${less}'" 1>&2
echo "LESS is:" 1>&2
echo "${less}" | od -bc
return 0
fi
LESS="${less}" man $1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment