Linux Groff Command Examples to Create Formatted Document

by Himanshu Arora on September 27, 2012

Groff stands for GNU troff. Groff is typically used to create man pages. But, it does more than that. Its an old school utility which has been around for almost 20 years but still holds its presence.

As a formatting and typesetting engine, this utility acts like a compiler for the input supplied (plain text content and formatting commands) to it and it creates a formatted document as its output. In this article we will understand the basics of this command by discussing some common groff requests along with examples.

Here is the syntax of this command :

groff [-abcegiklpstzCEGNRSUVXZ] [-d cs] [-D arg] [-f fam] [-F dir] [-I dir] [-K arg] [-L arg] [-m name] [-M dir] [-n num]
[-o list] [-P arg] [-r cn] [-T dev] [-w name] [-W name] [file ...]
groff -h | –help
groff -v | –version [option ...]

1. Basic Groff Example

This example uses groff utility to create a document with a single line without any formatting.

$ groff > grof_output_1
Hi, this is a Line.

This command would produce a PostScript file (PS file) that can be opened in a document viewer.

2. Introduce line spacing using .sp command

If a line space needs to be introduced, use .sp command followed by the number of line spaces required.

$ groff > grof_output_2
.sp 10
This line will occur after 10 spaces.
Thanks.

3. Request a break using .br command

For a regular line break, use .br command as shown below.

$ groff > grof_output_3
Hi,
.br
Welcome to TGS!

4. Change line spacing using .ls command

Line spacing for a document can be changed using .ls command followed by the number of line spacing to be introduced. By default the Line spacing is 1.

$ groff > grof_output_4
.ls 2
Hi,
.br
Welcome to TGS!

This document will look like the following when viewed in a document viewer:

5. Introduce a break page using .bp command

If it is intended to have the next line or paragraph from the next page of the document then .bp command is used.

$ groff > grof_output_5
Hi,
.bp
Welcome to TGS!

6. Center lines using .ce command

If some lines need to be pushed towards the center of the document then .ce command can be used.

The argument to .ce determines how many lines need to be pushed to center.

$ groff > grof_output_6
.ce 1
TGS DOCUMENT
Hello and welcome to TGS.

7. Set indentation using .in command

If indentation needs to be changed then this can be done through .in command followed by the indentation index.

$ groff > grof_output_7
Hi,
.br
.in 4
Welcome to TGS!

This document will look like the following when viewed in a document viewer:

8. Combine .br and .ce using .ti

This command can be used to break the text and center the immediate next line to ‘n’ steps. Where ‘n’ is the argument passed to .ti command.

$ groff > grof_output_8
Hi,
.ti 5
Welcome to TGS!

9. Set page offset using .po command

If page offset ie space between left edge of the document paper and left margin in the document needs to be set then .po command can be used.

$ groff > grof_output_9
.po 10
Hi,
.br
Welcome to TGS!

10. Set the font size using .ps command

If the text needs to be increased or decreased in size then .ps command can be used. Please note that you can precede the argument to .ps with – to decrease the font size

$ groff > grof_output_10
Hi,
.ps 20
Welcome to TGS!

This document will look like the following when viewed in a document viewer:


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 3 comments… read them below or add one }

1 Jalal Hajigholamali September 27, 2012 at 9:25 am

Hi,

Thanks a lot,
Very useful article,,,

2 Júlio Hoffimann Mendes September 28, 2012 at 4:40 am

Good to know about this tool, thanks.

Regards,
Júlio.

3 J.O. October 2, 2012 at 11:18 am

Does anybody know if there is a mobi or epub output module. Kinda like the tbl preprocessor.

Leave a Comment

Previous post:

Next post: