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 [...] 
6
  • This post on the Meta site 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. Commented May 16, 2016 at 19:44
  • Possible duplicate of View a man page in a specific section Commented May 16, 2016 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. Commented May 16, 2016 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. Commented May 16, 2016 at 20:01
  • Once you have the content, redirecting it is trivial. Commented May 16, 2016 at 20:28

4 Answers 4

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.

1
  • Thank you. My best answer choice is built on the length of the command :) Commented May 16, 2016 at 20:32
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?)

10
  • What does "/+" before the string I'm looking for stand for? Commented May 16, 2016 at 20:19
  • You mean +/? Did you run these two commands and read the explanations? Or are you asking about the backslash before +cmd? Commented May 16, 2016 at 20:50
  • Just a nitpick, but this assumes the pager is less. coffeMug's answer is better in that respect. Commented May 17, 2016 at 7:15
  • @StephenKitt The two answer are both right... The only thing that, in my opinion, is (a bit) different is the length... Commented May 17, 2016 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.) Commented May 17, 2016 at 16: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.

5
  • 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). Commented May 16, 2016 at 20:05
  • How can I open a manual page through vim? Thanks. Commented May 16, 2016 at 20:05
  • man cowsay > temp.txt ; vim temp.txt Commented May 16, 2016 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. Commented May 16, 2016 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). Commented May 16, 2016 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).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.