Minimal example:
man git | cat
Real example:
man git | grep --color=always -C 3 "pathspec"
FWIW, I've tried --pager="cat"
using various pagers, as well as piping to various pagers. I've also tried using vimcat
with the same, but unfortunately it freezes. I've even tried using unbuffered
. All to no avail.
Perhaps it's possible using -T
or {g,n,t}roff
, but I'm not sure how? Is there a way I can pipe to less
/vim
/etc and have it pass through to stdout with formatting (colors/etc) and without paginating?
Edit: Thanks to Stéphane's help below, I now have a beautiful way of easily searching through (and highlighting!) all matched man pages. This is a game changer for me. Thank you, Stéphane!
For those who might be interested, here is the key part of the script (no doubt it can be improved; feedback is always welcome):
Edit: I've now incorporated Stéphane's additional feedback. Huge improvement!
Edit: I've further improved this solution to work with both Ubuntu 20.04.4 LTS and macOS 12.6, by instead piping to ul
.
#!/bin/bash
arg_search="$1"
typeset -A seen=()
while IFS= read -ru3 filename; do
if (( ! seen[$filename]++ )); then
# 'man -awK' sometimes returns bogus results due to searching through
# everything, including comments in the man source file, so filter
# these out. Note that this creates a secondary problem: legitimate
# results may get filtered out when the search term is two or more
# words spanning across lines. This can be solved with a multiline
# regex using pcre2grep -M 'search\s+term
, but this is perhaps
# growing beyond the scope of things here.
{ man --no-hyphenation --no-justification --regex -- "$filename" |
grep -iqE -- "$arg_search"; } || continue
# Print filename. Fill terminal width with highlighted background color.
filename_bar="$(printf '%s\t' "$filename" | expand -t $(tput cols))"
printf '\e[1;103;30m%s\e[0m\n' "$filename_bar"
# Per Stéphane's feedback, here's a cleaner version of the same for zsh:
# psvar=${(mr[COLUMNS])filename} print -P '%K{yellow}%F{black}%1v%k%f'
MAN_KEEP_FORMATTING=1 man \
--no-hyphenation --no-justification --regex -- "$filename" | \
ul | \
grep -iE --color=always -- "$arg_search"
fi
# 'unbuffer' seems to be required for macOS in order to unbuffer the output,
# as 'stdbuf -oL -eL' doesn't seem to work. Install on macOS using
# 'brew install expect' [sic], which contains unbuffer. This otherwise
# doesn't hurt anything on Ubuntu 20.04.4 LTS. Unbuffering the output of
# 'man -awK' is important because it can take a long time to run.
done 3< <(unbuffer man -awK --regex -- "${arg_search}")
cat
or/bin/cat
as a pager does a nice piping intogrep
:man --pager="cat" git | grep -C 3 "pathspec"
– White Owl Sep 10 '22 at 01:05bash -c
, i.e.. usingbash -c "foo"
instead ofbash -c "$(cat <<EOF foo; EOF; )"
? – Ed Morton Sep 10 '22 at 13:00