4 ms·
If you want a fast way to read the EXAMPLES section only for a command, here is a shell function which creates an ‘eg’ command which only displays the “EXAMPLES
by teddyh 10y ago
If you want a fast way to read the EXAMPLES section only for a command, here is a shell function which creates an ‘eg’ command which only displays the “EXAMPLES” section of manual pages:
eg(){
MAN_KEEP_FORMATTING=1 man "$@" 2>/dev/null \
| sed --quiet --expression='/^E\(\x08.\)X\(\x08.\)\?A\(\x08.\)\?M\(\x08.\)\?P\(\x08.\)\?L\(\x08.\)\?E/{:a;p;n;/^[^ ]/q;ba}' \
| ${MANPAGER:-${PAGER:-pager -s}}
}
Usage:
$ eg tar
EXAMPLES
Create archive.tar from files foo and bar.
tar -cf archive.tar foo bar
List all files in archive.tar verbosely.
tar -tvf archive.tar
Extract all files from archive.tar.
tar -xf archive.tar
$
- eriknstr 10y agoHere's mine: examples () { man $1 | less +/^EXAMPLES } Usage: $ examples su EXAMPLES su -m man -c catman Starts a shell as user man, and runs the command catman. You will be asked for man's password unless your real UID is 0. Note that the -m option is required since user “man” does not have a valid shell by default. In this example, -c is passed to the shell of the user “man”, and is not interpreted as an argument to su. su -m man -c 'catman /usr/share/man /usr/local/man' Same as above, but the target command consists of more than a single word and hence is quoted for use with the -c option being passed to the shell. (Most shells expect the argument to -c to be a single word). su -m -c staff man -c 'catman /usr/share/man /usr/local/man' Same as above, but the target command is run with the resource limits of the login class “staff”. Note: in this example, the first -c option applies to su while the second is an argument to the shell being invoked. su -l foo Simulate a login for user foo. su - foo Same as above. su - Simulate a login for root.
- teddyh 10y agoYou probably still want the MAN_KEEP_FORMATTING=1 part, to keep colorization and bolding etc. in the manual page. Also, your solution does not respect the user’s pager preference; the user might prefer to read man pages in “w3m”, for instance.
- eriknstr 10y ago>You probably still want the MAN_KEEP_FORMATTING=1 part, to keep colorization and bolding etc. in the manual page Underlining is the only formatting I care about and that works without MAN_KEEP_FORMATTING on FreeBSD. I don't have colorization enabled in man pages on my laptop, and also I don't have bolding enabled either. I like it this way. >your solution does not respect the user’s pager preference; the user might prefer to read man pages in “w3m”, for instance. The pager preference of the user in this case is `less`. I know because the user happens to be myself :^)