6

How can I get contents of a man page without the text being wrapped around column 80 or so? I'd prefer the output to have proper formatting, line bolding, underscores, fixed-width font for examples etc.

However, all the command-line utilities and online man page resources provide either wrapped (http://linux.die.net) or un-formatted (man mmap | col -bx > mmap.txt), or improperly formatted (man -t mmap | ps2pdf - mmap.pdf) versions.

How can I get the properly formatted unwrapped text from a man page to be used for further booklet printing?

mbaitoff
  • 5,101
  • Look at the link I posted: man -P cat command_name. – slm Jan 30 '14 at 05:34
  • @slm that won't do the formatting, the OP wants to have his cake and eat it, both markup and unwrapped. man -P cat won't do it and neither will any of the other answers on the linked Q. – terdon Jan 30 '14 at 05:42
  • Change the pager to whatever you want. Do this instead to get the formatting + wrapping: man -P less command_name. Is this not what you want? – slm Jan 30 '14 at 05:43
  • 2
    Out of interest, what is "improperly formatted" about the ps2pdf output? Do you mean you want to save it to a file for later viewing on a screen of any size? Or you want to make man look better when you run it on a large screen? – Mikel Jan 30 '14 at 05:51
  • @Mikel: "Improperly" means that the code sample is rendered using the same proportional font as the main text. Looking at the proportional source code has quite an acid-in-the-eyes effect for me, which I find "improper". – mbaitoff Jan 30 '14 at 06:08
  • 1
    Hmm. Well, if you look at the input file, e.g. /usr/share/man/man2/mmap.2.gz, you'll notice there's not a lot of markup to help tell what's English prose, and what's a code sample. How would you tell the difference? – Mikel Jan 30 '14 at 06:14
  • 1
    I'm no pro at markup reading, but the man2html definitely distinguish English prose from source code in "EXAMPLE" section and renders it the correct way. Maybe th .nf markup? – mbaitoff Jan 30 '14 at 06:43
  • 1
    @mbaitoff code sample being rendered using proportional font is more likely issue of your formatter. For example I'm sitting at two computers ATM with different Linux distributions and on one man2html formats it flawlessly while on the other one it has issues with code sample (single line breaks are not interpreted verbatim). – peterph Jan 30 '14 at 11:25
  • @mbaitoff Good point. So what's missing in the man2html output? It's soft wrapped, but that doesn't matter in HTML: only tags such as <p> and <br> cause actual line breaks. – Mikel Feb 02 '14 at 20:23
  • @Mikel Looks like the man2html is the most appropriate solution to my question, and I marked the corresponding answer as accepted. – mbaitoff Feb 03 '14 at 05:45

4 Answers4

6

You have not specified your desired output format but from the things you've tried, it looks like you're not picky. This will produce correctly formatted, unwrapped html but it needs to be run on the actual man page file.

So, first locate the man file you're interested in:

$ man -w mmap
/usr/share/man/man2/mmap.2.gz

Them, run man2html on it:

man2html /usr/share/man/man2/mmap2.2.gz > mmap.html

Or, simply

zcat $(man -w mmap) | man2html > mmap.html

The output looks like this:

enter image description here

man2html was available in the Debian repository, I installed it with sudo apt-get install man2html.

Once you have it in HTML, you can translate to other formats easily enough: Actually, these won't work, they'll wrap the line automatically again.

man2html /usr/share/man/man1/grep.1.gz | html2ps > grep.ps
man2html /usr/share/man/man1/grep.1.gz | html2ps | ps2pdf14 - grep.man.pdf

`

terdon
  • 242,166
3

You can skip installing man2html and use groff to do the HTML conversion as well.

$ man -w netstat | xargs zcat | groff -T html -man > netstat.html

Then use elinks or some terminal web browser to view them.

$ elinks netstat.html

     ss of elinks

You can skip generating a file entirely if you use lynx as your web browser, which has a -stdin feature for reading in HTML.

$  man -w netstat | xargs zcat | groff -T html -man | lynx -stdin

     ss of lynx

slm
  • 369,824
3

Depending on exactly what you want the output to look like, and what system you're on, here's some things that could be useful.

  1. Setting line length via the .ll macro
  2. Setting left justification instead of block justification .ad l
  3. Using man -Tutf8 to tell man to output terminal formatting, even when redirecting stdout.

On my system, I can achieve this by modifying /usr/share/groff/current/tmac/tty.tmac and running man -Tutf8.

But you said you wanted text in a proportional font but code in a fixed-width font. Most terminals don't have the ability to switch between different fonts, and man pages don't carry any markup to say what's code and what human language. So your best bet is to find a document in another format for display in a browser or graphical viewer.

Probably the best two places for such documentation are

But there are others, depending on the command or function you're trying to look up.

Mikel
  • 57,299
  • 15
  • 134
  • 153
  • Forgot to say that I need the formatted unwrapped output for printing purposes. Dealing with macros and manual markup is not quite what I need. – mbaitoff Jan 30 '14 at 06:46
  • @mbaitoff, I'm quite certain that your printer is not a ticker tape. So you can't print unwrapped text no matter what you do. It has to get wrapped somewhere, whether your printer driver does it, your PDF converter, or your source text file. – Wildcard Apr 06 '16 at 23:14
  • I would suggest you play with printing from a terminal set to various widths, and see which gives the best results on the actual medium. It shouldn't be too hard to hack your problem that way. – Wildcard Apr 06 '16 at 23:15
1

I can't tell if you want some "special" formatting that's different from standard formatting that's done using ANSI escape codes:

I'd prefer the output to have proper formatting, line bolding, underscores, fixed-width font for examples etc.

(does "proper just mean "the normal way that man does it"?)

But... I will answer with regards to this part of your question:

How can I get contents of a man page without the text being wrapped around column 80 or so?

Just use:

MANWIDTH=9999 man mmap > mmap.txt

or:

COLUMNS=9999 man mmap > mmap.txt

If you need to do something fancier, you can use groff directly, but it doesn't seem to respect the COLUMNS variable. This awesome answer provided a solution: use the arguments -rLL <number> and -rLT <number> to groff (apparently undocumented, but man itself uses them).

For example, I use the following function to dump man pages to plain-text (intentionally disabling the formatting that you want to preserve, but just as an example):

man2txt() {
    groff -t -e -mandoc -Tascii -a -rLL=9999 -rLT=9999 "$(man -w "${1}")"
}

You could remove the -Tascii -a and add in whatever groff options you want, if you do in fact want to do anything special in that regard.

Sean
  • 123