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
  • [This post on the Meta site](http://meta.unix.stackexchange.com/q/3960/135943) may answer your question. I'm not sure if you're just trying to open a man page, jumping to a particular spot, or if you actually need to output the man page contents to stdout (your terminal). The link assumes the former. – Wildcard May 16 '16 at 19:44
  • Possible duplicate of [View a man page in a specific section](http://unix.stackexchange.com/questions/157838/view-a-man-page-in-a-specific-section) – Thomas Dickey May 16 '16 at 19:50
  • 1
    @ThomasDickey The page you linked spokes about man pages with the same name, e.g. write 1 is send a message to another user, write 2 is write to a file descriptor. – untitled May 16 '16 at 19:56
  • @ThomasDickey - I think the OP wants to access a specific section name within a single man page and not read a page from a specific numerical man section, which is what the answer you link to seems to do. – garethTheRed May 16 '16 at 20:01
  • Once you have the content, redirecting it is trivial. – Thomas Dickey May 16 '16 at 20:28
  • I see (I was thinking about one of the pages I noticed while answering [this](http://unix.stackexchange.com/questions/268730/easy-way-to-just-print-the-man-page-intro-or-description), e.g., [this page](http://unix.stackexchange.com/questions/180639/is-there-way-to-see-man-document-only-for-specified-option-of-a-command). Too late to amend my vote... – Thomas Dickey May 16 '16 at 21:46

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,654
  • 13
  • 44
  • 58
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
  • 35,316
  • 26
  • 130
  • 258
  • What does "/+" before the string I'm looking for stand for? – untitled May 16 '16 at 20:19
  • You mean `+/`? Did you run these two commands and read the explanations? Or are you asking about the backslash before `+cmd`? – Wildcard May 16 '16 at 20:50
  • Just a nitpick, but this assumes the pager is `less`. [coffeMug's answer](http://unix.stackexchange.com/posts/283516) is better in that respect. – Stephen Kitt May 17 '16 at 07:15
  • @StephenKitt The two answer are both right... The only thing that, in my opinion, is (a bit) different is the length... – untitled May 17 '16 at 16:23
  • @SimoneBonato Try installing `most` and setting `PAGER=most` in your environment to see what I'm talking about. (Or just setting `PAGER=more`, without installing `most`.) – Stephen Kitt May 17 '16 at 16:27
  • @StephenKitt the coffeMug's answer you are referring to, is that about this question? Because your link gives me a 404 error. – untitled May 17 '16 at 16:30
  • @SimoneBonato oops, sorry, yes, it's [this answer](http://unix.stackexchange.com/a/283516/) to this question. – Stephen Kitt May 17 '16 at 16:40
  • @StephenKitt As far as I have just learnt, there aren't big differences between less and most with `man` (with more I can't go back). But thanks to `-c` (with most), my research can be case sensitive :) – untitled May 17 '16 at 17:01
  • @SimoneBonato my point isn't that `most` is better than `less`, it's that coffeMug's answer works regardless of the value of `PAGER` (because it overrides the pager), whereas Wildcard's answer only works if the pager is `less`. So coffeMug's answers seems more general to me. – Stephen Kitt May 17 '16 at 17:05
  • Let us [continue this discussion in chat](http://chat.stackexchange.com/rooms/39913/discussion-between-simone-bonato-and-stephen-kitt). – untitled May 17 '16 at 17:27
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,095
  • 7
  • 21