gnroff Command on Linux



The gnroff command in Linux is a tool used from the command line to format and display documents. The term gnroff stands for GNU troff, which is a text formatting utility. It processes documents written in the troff language and produces output that can be viewed on the terminal or saved as a plain text file. It is a simpler version of troff, which is used for creating more complex documents like PDFs or PostScript files.

gnroff, on the other hand, is for text-based devices like terminals. In this tutorial, well discuss what is gnroff and how it works in Linux.

Table of Contents

Here is a comprehensive guide to the options available with the gnroff command −

What is gnroff Command?

The gnroff command formats text for terminal display. It manages tasks like text alignment, indentation, and font styles. It's often used to format and view manual pages on Unix-like systems. Additionally, it supports macros that simplify the formatting process.

Standard macro packages, such as man or mdoc, help structure technical documents.

Syntax of gnroff Command

To use this utility in Linux, you must follow the below-provided syntax −

gnroff [options] fileName

Here, gnroff is a command itself that is used to format text for terminal output. Options are the flags or arguments that illustrate how gnroff processes the file. fileName is the target file that contains the text we want to format using gnroff.

Options gnroff Command

The following table shows various options accepted by the gnroff command and their descriptions −

Option Description
-man It specifies that the input file uses the man macro package.
-mdoc This option shows that the input file uses the mdoc macro package.
-T[device] It defines the output device or format (e.g., -Tascii for plain text, -Tpdf for PDF).
-P[page-size] It specifies the page size for formatting.
-v It returns a detailed output for debugging.
-E This option forces the output to use a specific encoding.

Examples of gnroff Command in Linux

To use the gnroff command in Linux, open the terminal, prepare your text file with the appropriate macros, and run the gnroff command to format the file. You can also redirect the output to a file. Lets understand this command with the following examples −

  • How to Format a Manual Page for Terminal Display?
  • How to Format a Document with mdoc Macro Package?
  • How to Output in Different Device Formats?
  • How to Format and Redirect Output to a File?

How to Format a Manual Page for Terminal Display?

You can execute the gnroff command with the -man option to format a manual page written in nroff format. This option uses the man macro package to handle the formatting −

gnroff -man guide.1

The above command processes the file named guide.1 using the man macros. As a result, it produces a terminal-friendly output.

How to Format a Document with mdoc Macro Package?

We can run the following command to format a document using the mdoc macro package −

gnroff -mdoc guide.8

This command processes the guide.8 file using the mdoc macros, which are commonly used for documentation in the man pages.

How to Output in Different Device Formats?

In the below example, we use the -Tascii option to format a file in ASCII format −

gnroff -Tascii guide.1

This command will format the guide.1 in ASCII format.

How to Format and Redirect Output to a File?

We can format the given document using the gnroff command and save the output to a file instead of displaying it on the terminal −

gnroff -man guide.1 > formattedGuide.txt

This time, the gnroff command formats guide.1 and redirects the output to formattedGuide.txt file.

Conclusion

The gnroff command in Linux is a utility used to format text documents for terminal display. It is a simplified version of the troff command, specifically designed for text-based outputs such as terminal screens.

Unlike troff, which handles complex formatting for PDFs and PostScript files, gnroff focuses on straightforward text formatting tasks. It supports macro packages like man and mdoc, which help in structuring technical documents and manual pages. Key options include specifying the output format with -T[device], choosing the macro package with -man or -mdoc, and redirecting output to files.

In this tutorial, we covered how to use gnroff to format and view documents in the terminal, including practical examples and command options.

Advertisements