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?

10
  • Look at the link I posted: man -P cat command_name.
    – slm
    Jan 30, 2014 at 5:34
  • 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, 2014 at 5:51
  • 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, 2014 at 6: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, 2014 at 6: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, 2014 at 11:25

4 Answers 4

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

`

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

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.

3
  • 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, 2014 at 6: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 6, 2016 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 6, 2016 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.