-
-
Save alphapapa/3cba3ff196147ad42bac to your computer and use it in GitHub Desktop.
#!/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" |
#!/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" |
You should throw this in a proper repo so people can contribute back, I can see it being useful to others. I would have liked to pull request that stuff in.
For background I use the default bash in mac - but only because I never use it, I use ZSH as my primary shell. (I probably should update bash though...)
Default Bash:
❯ bash --version GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) Copyright (C) 2007 Free Software Foundation, Inc.
In terms of the infinite loop, I like you're right, I think I was only getting that when I was debugging the bash negative index issue. It seems that once $args[@] get down to nothing, it'll just return the command "man -w" which should succeed and get you out of the loop.
In terms of the feature, I just found out about a neat less argument "less -X" that pretty much does that. It's actually my default MANPAGER - it essentially makes it so it doesn't clear/reset the screen when you man. (Try mangle less -X
;))
You should throw this in a proper repo so people can contribute back, I can see it being useful to others. I would have liked to pull request that stuff in.
I think I will do that, thanks.
Wow, that Bash version is old! :)
In terms of the feature, I just found out about a neat less argument "less -X" that pretty much does that. It's actually my default MANPAGER - it essentially makes it so it doesn't clear/reset the screen when you man. (Try mangle less -X ;))
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.
That's great, thanks for sharing that!
Ah, I see that those were added in Bash 4.3. I guess I never had occasion to use them before, haha.
Good idea!
This one puzzles me: I can't seem to cause this. Could this be because of different Bash versions? I'm using Bash 4.3.11.
Nice! I guess I had never actually done that before. I think I'll try using egrep with multiple patterns here too.
There is one little feature I have working in my copy but haven't uploaded: when the matches fit in the current terminal, it causes less to exit immediately and leave the output on the screen. If it doesn't fit on the screen, it runs less normally and resets the screen on exit. I figured that not everyone would want that, but I find it pretty handy. What do you think?