7

This is a simple question. Trying my best to put it in simple word. Hope you get it without much confusion -

I read a man page for some command-switch/option which has a long description. I read it, press q to escape and try the option. But then I do need to go back to same man page section for some more info. So, I repeat the steps

I would like to know if there is any way to output the contents of man page onto the stdout for referring it quickly.

For example let's take short-one cal, may be something like

cat `man cal`

would be effective and help achieve the desired requirement.

Any hints?

mtk
  • 27,530
  • 35
  • 94
  • 130

4 Answers4

9

You could run the output of man into the col command and then pipe this to less. Once in less you can drop to a shell while still maintaining your location in less.

$ man col | col -b | less

Once in less you can use !bash to get to a prompt to do what you want. When you're done you type exit to return back to your location in less.

Example

Here's a demo showing the whole operation.

            ss of demo

Tell man to use a different pager

You can also tell man to use different pagers via the -P switch. So we could streamline the above method like so:

$ man -P less col

This is invoking man and then outputting the contents of the page to less, where again we can use !bash to get to a shell. To return we use the same steps as above, exit.

What's wrong with the default pager?

Actually nothing.

$ man col

It too can take the command !bash to escape to a shell, where again we can type exit to get back to the location where we were previously within the man page.

What else?

If you're feeling really crazy you can use vim as an alternative pager too. Setting this up is a bit of a task but it's doable, directions for doing so are here in the vim wikia topic titled: Using vim as a man-page viewer under Unix.

Don't let the above page fool you, it doesn't just cover vim methods. That topic covers dozens of ways you can change your man page pager in addition to using vim.

slm
  • 369,824
  • Ctrl+Z and fg should work too. I guess this depends on what exactly you want to do at that prompt. To be closer to what appears to be the intent of the OP's example, just remove the piping into less (man col | col -b). – user Dec 04 '13 at 09:02
  • Sorry to bother you. What are the explicit differences between man col | col -b | less and man col | less? Seems both of them return to original scroll position of man page after typing !bash and exit. – Ivan Chau Dec 04 '13 at 09:55
  • 1
    @IvanChau - the col -b is needed when you encounter a man page that is explicitly in troff format. It's discussed here a bit: http://unix.stackexchange.com/questions/15855/how-to-dump-a-man-page – slm Dec 04 '13 at 10:06
6

You can pipe the output of man through whatever command you like.

man cal | head
man cal | cat

If you want the man page (or at least the part you've scrolled through) to remain in the terminal's scrollback buffer when you exit less, run

LESS=-X man cal

(this may have strange effects on some terminals though.)

If you're reading a man page and want to try something out, the easiest way is to keep the man page open in one terminal and have a shell prompt in another terminal. If possible, arrange for both terminals to be visible on-screen at the same time. You can use Screen or Tmux if you can't run two terminal emulator windows for some reason.

If you want to do everything in the same terminal, you can suspend the man command by pressing Ctrl+Z, and go back to it with the command fg. If you have several suspended or backgrounded commands, fg resumes the latest to have been in the foreground; typing %1, %2, etc. resumes one designated by its job number.

1

As slm already pointed out, you can use col to filter reverse line feeds from the output of man, then pipe that output further. To output a few lines from the beginning of a manual page, you can combine this with head, e.g. to output the first twenty lines:

$ man cal | col -b | head -n 20

How about outputting each line which looks like an option description:

$ man cal | col -b | grep -A1 "^\s*-"
Thomas Nyman
  • 30,502
1

You are looking for

PAGER="cat" man cal

... at least that's the syntax for bash. The content of the variable PAGER is used to display the man page. If you set PAGER="cat" it will write it to stdout. If you set PAGER="pastebinit" you end up posting man-pages to pastebin.

Of course this only solves the "output the contents of man page onto the stdout"

Bananguin
  • 7,984