Complete Guide: Writing Professional man Pages with groff Formatting & Best Practices


7 views

Man pages follow a strict organizational structure divided into standard sections. The most common sections include:

  • NAME - Command name and one-line description
  • SYNOPSIS - Command syntax
  • DESCRIPTION - Detailed explanation
  • OPTIONS - Available command-line options
  • EXAMPLES - Usage examples
  • SEE ALSO - Related commands

The standard man page format uses groff (GNU troff) macros. Here are the key formatting codes:


.TH TITLE SECTION DATE SOURCE MANUAL
.SH SECTION_NAME
.B Bold text
.I Italic text
.PP
New paragraph
.TP
Tagged paragraph
.br
Line break

Let's create a simple man page for a fictional command called "greet":


.TH GREET 1 "June 2024" "v1.0" "User Commands"
.SH NAME
greet \- display greeting messages
.SH SYNOPSIS
.B greet
[\fB\-h\fR]
[\fB\-t\fR \fITYPE\fR]
[\fB\-n\fR \fINAME\fR]
.SH DESCRIPTION
.B greet
displays customizable greeting messages to the user.
.PP
The program supports multiple greeting types and personalized names.
.SH OPTIONS
.TP
.B \-h
Display help message
.TP
.B \-t \fITYPE\fR
Specify greeting type (morning/afternoon/evening)
.TP
.B \-n \fINAME\fR
Specify recipient name
.SH EXAMPLES
.TP
Display default greeting:
greet
.TP
Display evening greeting to John:
greet -t evening -n John
.SH SEE ALSO
.BR echo(1),
.BR printf(1)

For efficient man page development:

  1. Write in plain text editor with groff syntax
  2. Use man ./yourpage.1 to test locally
  3. Consider these tools for better workflow:
    • vim with groff syntax highlighting
    • pandoc for conversion from Markdown
    • ronn (Ruby-based man page generator)
  • Keep descriptions concise but complete
  • Use standard section headers
  • Maintain consistent formatting
  • Include practical examples
  • Cross-reference related commands
  • Test on actual terminal (80 chars width)

If you prefer writing in Markdown:


pandoc -s -t man input.md -o output.1

Basic Markdown to groff conversion rules:
- # becomes .SH
- *text* becomes \fItext\fR
- **text** becomes \fBtext\fR

Before distribution:


# Check syntax
groff -T utf8 -man yourpage.1 | less

# Verify installation
sudo cp yourpage.1 /usr/share/man/man1/
mandb
man yourpage

Linux man pages follow a specific structure using troff/groff formatting. The basic sections include:

.TH "COMMAND" "SECTION" "DATE" "VERSION" "DESCRIPTION"
.SH NAME
command \- brief description
.SH SYNOPSIS
.B command
.RI [ options ]
.RI [ file ]
...

Key troff macros for man pages:

  • .TH - Title header (must be first line)
  • .SH - Section heading
  • .B - Bold text
  • .I - Italic text
  • .PP - New paragraph
  • .TP - Hanging tag for option descriptions

Recommended approaches:

# Option 1: Direct editing with syntax highlighting
vim mycommand.1  # with groff syntax plugin

# Option 2: Markdown conversion
pandoc -s -t man README.md -o program.1

# Option 3: Help2man (auto-generate from --help)
help2man ./myprogram > myprogram.1

Sample man page for a fictional tool:

.TH MYTOOL 1 "2023-08-20" "v1.2.0" "User Commands"
.SH NAME
mytool \- processes data files efficiently
.SH SYNOPSIS
.B mytool
.RB [ \-v ]
.RB [ \-o
.IR output ]
.RI [ input ]
.SH DESCRIPTION
Processes input files with advanced algorithms.
.PP
Supports multiple file formats including JSON and CSV.
.SH OPTIONS
.TP
.B \-v
Enable verbose output
.TP
.BI \-o " file"
Specify output file

Follow these guidelines:

  • Keep descriptions concise but complete
  • List all options with clear explanations
  • Include EXAMPLES section with realistic use cases
  • Add BUGS section for known issues
  • Maintain consistent formatting throughout

Test and install your man page:

# Validate syntax
man ./mytool.1

# Install system-wide
gzip mytool.1
sudo install -m 644 mytool.1.gz /usr/share/man/man1/
sudo mandb

For complex projects with multiple man pages:

# API documentation (section 3)
.TH LIBFOO 3 "2023-08-20" "v1.0" "Library Functions"
# Configuration (section 5)
.TH FOO.CONF 5 "2023-08-20" "v1.0" "File Formats"