-
-
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")
This file contains hidden or 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
#!/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