WIPIVERSE All articles

fmt (Unix)

fmt is a standard command-line utility in Unix and Unix-like operating systems that acts as a simple text formatter. It reformats paragraphs of plain text so that lines fit within a specified maximum width, improving readability for documents, email messages, code comments, and other text-based content.

Overview

The primary purpose of fmt is to rewrap ("reflow") text paragraphs to fit within a specified line width. It reads text from one or more input files (or from standard input if no file is given, or if the file is specified as -) and writes the formatted result to standard output.

The utility treats consecutive non-blank lines as a single paragraph and reflows them together. Blank lines, changes in indentation, and other formatting cues mark paragraph boundaries. Words are never hyphenated or broken across lines.

History

The fmt command originated in BSD Unix around 1979, based on earlier text-formatting tools. It was designed as a simple text formatter for preparing documents and email. The command has since been included in the GNU Coreutils package and remains a standard component of virtually all Unix-like systems, including Linux distributions and macOS.

Syntax

fmt [-WIDTH] [OPTION]... [FILE]...

The -WIDTH notation is an abbreviated form of --width=DIGITS.

Common Options

  • -w, --width N — Sets the maximum output line width (default: 75 characters).
  • -g, --goal N — Specifies the goal width (the optimal line length), which defaults to 93% of the width.
  • -s, --split-only — Splits long lines but does not join short ones.
  • -u, --uniform-spacing — Uses exactly one space between words and two spaces after sentence-ending punctuation.
  • -c, --crown-margin — Preserves the indentation of the first two lines of each paragraph.
  • -t, --tagged-paragraph — Preserves the indentation of the first line of a paragraph, allowing it to differ from subsequent lines.
  • -p, --prefix STRING — Only reformats lines that begin with the given prefix, reattaching the prefix to reformatted lines.
  • --help — Displays a help message and exits.
  • --version — Displays version information and exits.

Behavior and Algorithm

The formatting algorithm aims for the goal width while staying within the maximum width, producing more natural-looking text than simply breaking lines at exactly the maximum width. Long words that exceed the width are not broken.

Common Uses

Typical applications of fmt include:

  • Formatting documentation and README files for consistent line length.
  • Rewrapping email text to fit within a comfortable reading width.
  • Cleaning up plain-text files in which line breaks have become inconsistent.
  • Enforcing uniform spacing in text (-u).
  • Breaking overly long lines in code comments (-s).

Limitations

fmt handles plain text only; it does not understand markup languages, code blocks, or special formatting structures. It does not preserve exact spacing or original formatting. Consequently, it may inadvertently break intentionally formatted text such as tables or source code. Different implementations (e.g., GNU versus BSD) may behave slightly differently in edge cases.

Availability

The fmt command is part of the GNU Coreutils package on Linux systems and is preinstalled on nearly all distributions. It is also available on macOS and other BSD-derived systems, and through package managers on other platforms (for example, via Homebrew's coreutils on macOS).

Browse

More topics to explore

    Browse all articles