fmt - macOS


Overview

The fmt command in macOS is a text utility used to reformat paragraph text to a specified width, optimizing readability. It’s primarily used for adjusting the line length of text files, such as plain text documents and source code comments. The command is most effective in preparing text for display in environments that do not support automatic line wrapping or for creating uniformly formatted text blocks in documents.

Syntax

The basic syntax of the fmt command is as follows:

fmt [-c] [-p prefix] [-s] [-t] [-u] [-w width] [goal [maximum]] [file ...]
  • file …: One or more filenames; if no file is specified, or if the file name “-” is given, fmt reads from standard input.

Options/Flags

  • -c: Centers the text instead of justifying it.
  • -p prefix: Sets each line in the output to begin with the string specified as prefix.
  • -s: Splits lines but does not join them. Useful for reflowing paragraphs with hard returns.
  • -t: Causes the indentation of the first two lines to determine the line width.
  • -u: Uniform spacing (i.e., one space between words and two after sentences).
  • -w width: Sets the width for line output, where the default is 75 characters. If goal and maximum are also set, width is ignored.
  • goal [maximum]: Optionally specify the target line length (goal) and the maximum line length (maximum), which fmt will attempt to achieve.

Examples

  1. Basic Formatting:
    Format a file named example.txt:

    fmt example.txt
    
  2. Specify Width:
    Format a text file to a width of 50 characters:

    fmt -w 50 example.txt
    
  3. Center Text:
    Center the contents of a text file:

    fmt -c example.txt
    
  4. Use with Standard Input:
    Format text coming from another command like echo:

    echo "This is a simple example of using fmt with echo." | fmt
    

Common Issues

  • Line Breaks Maintained: Sometimes, users might find that fmt preserves too many of the existing breaks or does not format paragraphs as expected. Use -s to force splitting without rejoining lines, which can help manage unexpected line breaks.
  • Prefix Formatting: Misuse of the -p option can lead to unexpected indentations if not correctly specified.

Integration

fmt can be integrated with other tools like sed, awk, or scripts to automate complex text manipulation tasks:

cat report.txt | fmt | awk '{print toupper($0)}' > output.txt

This example formats a text file, converts it to uppercase, and saves the output in another file.

  • fold: Similar to fmt but wraps each line to fit a specified width without trying to preserve the readability of paragraph structure.
  • pr: Formats text into pages and columns, adding headers, footers, and other formatting useful for printing.
  • awk, sed: Tools for more granar text manipulation and formatting.

Further documentation on fmt and related text formatting utilities can be found in the macOS man pages (man fmt) or through the GNU Coreutils documentation if using a GNU version of the tool.