Last active
June 21, 2021 16:03
-
-
Save alphapapa/3cba3ff196147ad42bac to your computer and use it in GitHub Desktop.
Mangle man pages to show just the parts you need (suitable for aliasing to "man")
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
#!/bin/bash | |
less_command='| less $less_no_init -aiF -Ps"Manual page\: $man_page (?ltline %lt?L/%L.:byte %bB?s/%s..? (END):?pB %pB\%..)"' | |
# Get section | |
if [[ $1 =~ [0-9]+ ]] | |
then | |
section=$1 | |
shift | |
fi | |
# Find the longest set of args that gets a man page | |
man_page=("$@") | |
while ! command man -w $(echo "${man_page[@]}" | tr " " "-") &>/dev/null | |
do | |
patterns+=( ${man_page[-1]} ) | |
unset man_page[-1] | |
done | |
# For some reason, using an array substitution in the prompt string in | |
# less_command isn't working, so work around that by turning it into a | |
# plain string | |
man_page="${man_page[@]}" | |
# Build grep command | |
if [[ ${patterns[@]} ]] | |
then | |
grep_command="| grep -i -C4 --color=always -- ${patterns[@]}" | |
fi | |
command="command man $section $man_page $grep_command" | |
# Check screen and output sizes | |
tty_lines=$(tput lines) | |
output_lines=$(eval "$command" | wc -l) | |
# If output < screen size, use less with -X | |
[[ $output_lines -lt $tty_lines ]] && less_no_init="-X" | |
# Display the page | |
eval "$command $less_command" |
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
#!/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" | |
else | |
grepCommand=" | 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
@derimagia
I think I will do that, thanks.
Wow, that Bash version is old! :)
Yep, that's what I was referring to. I made it so mangle only does that if the output fits on the screen, otherwise it uses a normal pager that resets the terminal. I just posted the version that does that, it's up at the top above the previous version.