Skip to content

Instantly share code, notes, and snippets.

@davemac
Forked from alphapapa/mangle
Created March 3, 2016 07:18
Show Gist options
  • Save davemac/34963b0493cf0e3ebb92 to your computer and use it in GitHub Desktop.
Save davemac/34963b0493cf0e3ebb92 to your computer and use it in GitHub Desktop.
Easily grep for and view relevant parts of man pages (suitable for aliasing to "man")
#!/bin/bash
# Get section
if [[ $1 =~ [0-9]+ ]]
then
section=$1
shift
fi
# Find the longest set of args that gets a man page
args=("$@")
while ! command man -w $(echo "${args[@]}" | tr " " "-") &>/dev/null
do
patterns+=( ${args[-1]} )
unset args[-1]
done
# Build grep command
if [[ ${patterns[@]} ]]
then
grepCommand="| grep -i -C4 --color=always -- ${patterns[@]} | less -RFX"
fi
# Do it
eval "command man $section ${args[@]} $grepCommand"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment