Formatting the man command output for printing

  • Post author:
  • Post last modified:May 12, 2022
  • Reading time:4 mins read

1. Limiting the width of Linux man command output

Quite often, we need to print the output of the Linux man command. One way is to redirect the man command output to a file, open the file in LibreOffice Writer and export to PDF. The PDF can, later, be printed. However, by default, the man command output comprises of long lines which look great on computer's display but wrap over to next line when printed. One way is to manually format the text in LibreOffice Writer. But this is tedious. How to reduce the width of man command output? If the man command output could be limited to, say, 80 columns, then the text would not wrap over to next line in LibreOffice Writer and the man command output file could be exported to the PDF straightaway.

The solution lies in setting the environment variable MANWIDTH. For example, the following commands need to be given from the shell.

$ export MANWIDTH=80
$ man command > command.txt

Here, command is a Linux command like ls. The width of the output text is limited to MANWIDTH, which is 80 characters in this case. After this the file command.txt can be opened in LibreOffice Writer. You can add page number to pages by, first, click, Insert -> Footer -> Default. And, then, click inside the footer and select center or right justification of text in the footer. Finally, click, Insert -> Fields -> Page Number. Lastly, export the file to the PDF by clicking, File -> Export as PDF.

2. Filtering out control characters from man output

Some Unix systems like Solaris embed the man output with control characters like backspace (08) in order to make certain characters bold. For example, the sequence A (0x41) backspace (0x08) A (0x41) prints A in bold in the man command output on terminals. But when we open the redirected man command output file in an editor, we see a lot of control and repeating characters. We can get rid of these by using the col command. For example,

$ man command > command.txt
$ col -b < command.txt >

The col command can only read and write standard input and output respectively. It does not read and write files directly.

3. Deleting blank lines

The file might have multiple blank lines. These can be deleted by using the Perl one liner,

$ perl -00pe0 < > command.out

4. Deleting lines containing a string

Some systems print a header line like, User Commands on each page. These lines are easily deleted using the Perl one liner,

perl -ni -e ‘print unless /pattern/’ filename

This deletes the lines containing pattern in the file, filename, in situ.


Karunesh Johri

Software developer, working with C and Linux.