3

From terminal, how can I print to output a specific section of the result of man something?

For example, if I wanted to get some information about the return value of the C function write, I'd like to see something like this:

RETURN VALUE
       On  success,  the  number  of bytes written is returned (zero indicates
       nothing was written).  It is not an error if  this  number  is  smaller
       than the number of bytes requested; this may happen for example because
       the disk device was filled.  See also NOTES.

       On error, -1 is returned, and errno is set appropriately.

       If count is zero and fd refers to a  regular  file,  then  write()  may
       return  a failure status if one of the errors below is detected.  If no
       errors are detected, or error detection is not  performed,  0  will  be
       returned  without  causing  any  other effect.  If count is zero and fd
       refers to a file other than a regular file, the results are not  speci‐
       fied.

ERRORS
       EAGAIN The  file descriptor fd refers to a file other than a socket and
          has been marked nonblocking (O_NONBLOCK), and  the  write  would
          block.  See open(2) for further details on the O_NONBLOCK flag.

       EAGAIN or EWOULDBLOCK
          The  file  descriptor  fd refers to a socket and has been marked
          nonblocking   (O_NONBLOCK),   and   the   write   would   block.
[...]

instead of:

WRITE(2)                   Linux Programmer's Manual                  WRITE(2)

NAME
       write - write to a file descriptor

SYNOPSIS
       #include <unistd.h>

       ssize_t write(int fd, const void *buf, size_t count);

DESCRIPTION
       write()  writes  up  to  count bytes from the buffer pointed buf to the
       file referred to by the file descriptor fd.

       The number of bytes written may be less than  count  if,  for  example,
       there  is  insufficient space on the underlying physical medium, or the
       RLIMIT_FSIZE resource limit is encountered (see setrlimit(2)),  or  the
       call was interrupted by a signal handler after having written less than
       count bytes.  (See also pipe(7).)

       For a seekable file (i.e., one to which lseek(2) may  be  applied,  for
       example,  a  regular file) writing takes place at the current file off‐
       set, and the file offset is incremented by the number of bytes actually

[...]
untitled
  • 141

4 Answers4

3

You can use -P flag of man program to use a pager to display pages. For example you can use less as the pager program with flag -p to search for the pattern ERROR happening in the beginning of the line inside the man page:

man -P 'less -p ^ERRORS' symlink

This opens man page of symlink and jumps directly to the ERRORS section of it.

Vombat
  • 12,884
2

To quote my own post from Meta:

Linking to man pages

I already have a favored method for this, which you can read about in the less man page in two places:

LESS='+/\+cmd' man less

and

LESS='+/LESS[[:space:]]*Options' man less

(See what I did there?)

Wildcard
  • 36,499
0

If you just open a manpage like:
man cowsay
You can then type:
/AUTHOR
to find and jump to the AUTHOR line, for example. Or:
/myFunction
to find instances of myFunction in the manpage.
(If there are multiple instances, you can press n to go to the next instance)

Also, if you're in a man page, you can type h and get a summary of less commands like below. I cut them off after parts I thought were relevant to you, but there's more.

                  SUMMARY OF LESS COMMANDS

      Commands marked with * may be preceded by a number, N.
      Notes in parentheses indicate the behavior if N is given.

  h  H                 Display this help.
  q  :q  Q  :Q  ZZ     Exit.
 ---------------------------------------------------------------------------

                           MOVING

  e  ^E  j  ^N  CR  *  Forward  one line   (or N lines).
  y  ^Y  k  ^K  ^P  *  Backward one line   (or N lines).
  f  ^F  ^V  SPACE  *  Forward  one window (or N lines).
  b  ^B  ESC-v      *  Backward one window (or N lines).
  z                 *  Forward  one window (and set window to N).
  w                 *  Backward one window (and set window to N).
  ESC-SPACE         *  Forward  one window, but don't stop at end-of-file.
  d  ^D             *  Forward  one half-window (and set half-window to N).
  u  ^U             *  Backward one half-window (and set half-window to N).
  ESC-)  RightArrow *  Left  one half screen width (or N positions).
  ESC-(  LeftArrow  *  Right one half screen width (or N positions).
  F                    Forward forever; like "tail -f".
  r  ^R  ^L            Repaint screen.
  R                    Repaint screen, discarding buffered input.
        ---------------------------------------------------
        Default "window" is the screen height.
        Default "half-window" is half of the screen height.
 ---------------------------------------------------------------------------

                          SEARCHING

  /pattern          *  Search forward for (N-th) matching line.
  ?pattern          *  Search backward for (N-th) matching line.
  n                 *  Repeat previous search (for N-th occurrence).
  N                 *  Repeat previous search in reverse direction.
  ESC-n             *  Repeat previous search, spanning files.
  ESC-N             *  Repeat previous search, reverse dir. & spanning files.
  ESC-u                Undo (toggle) search highlighting.
  &pattern          *  Display only matching lines
        ---------------------------------------------------
        Search patterns may be modified by one or more of:
        ^N or !  Search for NON-matching lines.
        ^E or *  Search multiple files (pass thru END OF FILE).
        ^F or @  Start search at FIRST file (for /) or last file (for ?).
        ^K       Highlight matches, but don't move (KEEP position).
        ^R       Don't use REGULAR EXPRESSIONS.
 ---------------------------------------------------------------------------

                           JUMPING

  g    ESC->       *  Go to last line in file (or line N).
  p  %              *  Go to beginning of file (or N percent into file).

If you just want to more easily read big man pages, this should work.

  • That's not Vim, it's less. It even says so right at the top. Those are not Vim commands (though there is some overlap). – Wildcard May 16 '16 at 20:05
  • How can I open a manual page through vim? Thanks. – untitled May 16 '16 at 20:05
  • man cowsay > temp.txt ; vim temp.txt – infixed May 16 '16 at 20:07
  • Thanks @Wildcard. I'm an idiot. Total brain fart. I learned vi and think of man pages and less as using all "vim commands" so I said vim in my post without thinking. Good catch. – Rand0mAcc3ss May 16 '16 at 20:09
  • @SimoneBonato you can open a manpage as usual, and from less, press v to open it in an editor (vim if $EDITOR or $VISUAL is set up appropriately). – Stephen Kitt May 16 '16 at 20:58
0

Taking care of trees you should re-think whether to print manual pages in whole or parts, specifically in times where they might change every few months.

Instead, as suggested in other answers, you could (learn to) use the pager (e.g. less) to search back and forth for the information you need. Typically the structure of man pages help you to find that more easily.

Also some programs allow you to open manual pages "inside", like Emacs: There you can use the "M-x man" (or "M-x woman") command to open a manual page and then use all the editor functions to navigate in it (Emacs also has a print-region function, but explaining that would be off-topic for this answer).

U. Windl
  • 1,411