VMware ESXi and vSphere Cluster Management

Reformat Paragraphs with the Linux fmt Command

Learn how to use the Linux fmt command to reflow prose paragraphs, set custom line widths, read from standard input, and safely save formatted output.

The Linux fmt command reformats prose-like text by wrapping lines into more uniform lengths. It is useful when a text file contains lines that are too long, too short, or wrapped inconsistently.

fmt is designed for paragraphs, not arbitrary structured data. It does not understand programming-language syntax, table columns, markup semantics, or configuration-file structure.

What fmt Does

fmt is a Unix/Linux utility that reflows text paragraphs into lines of a chosen width. To reflow text means to join existing lines and wrap the words again according to a target line length.

For example, a paragraph may have been entered as one very long line or split into uneven lines. fmt reads the text, joins and rearranges lines within a paragraph, and writes newly wrapped output.

A paragraph is a group of text lines treated as one reflowable unit. In ordinary text files, a blank line commonly separates one paragraph from the next. Blank lines are generally preserved as paragraph boundaries, so each paragraph is formatted independently.

Words are kept together wherever possible. Therefore, the output lines may not all contain exactly the same number of characters. The selected width is best understood as a maximum target rather than a promise that every line will be filled precisely.

Default Line Width

When no width is specified, fmt uses a default line width of 75 characters. The width includes the characters placed on each generated output line, including letters, spaces, punctuation, and other characters that appear in the output.

fmt notes.txt

This command reads notes.txt, reformats its paragraphs to the default width, and sends the result to standard output.

Format a Text File

Give a text-file path as an argument:

fmt notes.txt

Standard output is the normal destination for command output, usually the terminal. Running the command this way displays the reformatted text but does not automatically edit notes.txt.

To save the result in a separate file, use shell redirection. Redirection is syntax that sends command output to a file or reads command input from a file.

fmt notes.txt > notes-formatted.txt

The source file remains unchanged, while notes-formatted.txt receives the reformatted output.

Set a Custom Line Width with -w

The -w option selects the desired maximum output width:

fmt -w WIDTH FILE

Replace WIDTH with a number of characters.

Narrow output

fmt -w 40 notes.txt

This wraps prose near 40 characters per line, which can be useful for a narrow terminal, a compact message, or a layout with limited horizontal space.

Wider output

fmt -w 100 notes.txt

A wider width can reduce the number of lines in documentation or suit a display with more horizontal room. Other useful widths include 60 characters for narrow documentation and 72 characters for email-style text.

Because fmt normally keeps words intact, a line can be shorter than the requested width when adding the next word would exceed it.

Read Text from Standard Input

Standard input is text supplied to a command through the terminal, a pipe, or input redirection. Instead of naming a file, you can send input to fmt.

Input redirection

fmt < notes.txt

This reads the contents of notes.txt through standard input and displays the formatted result in the terminal. It does not modify the file.

Piping output from another command

A pipe passes one command's output as another command's input. For example:

printf '%s\n' 'A long line of prose that needs to be wrapped into a more readable paragraph format.' | fmt -w 40

Here, printf produces the text, and fmt receives it through the pipe and prints lines wrapped near 40 characters.

Viewing output in the terminal and saving output are different actions. Add redirection when you want a file:

printf '%s\n' 'A long line of prose that needs to be wrapped into a more readable paragraph format.' | fmt -w 40 > formatted.txt

Keep Paragraphs Distinct

Blank lines normally separate paragraphs. Consider a file containing two prose sections with an empty line between them:

fmt -w 50 article.txt

fmt reflows each section independently and retains the blank-line separation. If two sections that should be separate do not have a clear blank line between them, the input may be treated as one paragraph block.

Common fmt Command Patterns

GoalCommand patternResult
Use the default width on a filefmt FILEReformats paragraphs using the default 75-character width and prints the result.
Choose a custom width with -wfmt -w WIDTH FILEReformats paragraphs using the selected maximum line width.
Read from standard inputfmt < FILEReads file content through standard input and prints formatted output.
Write output to a new filefmt -w WIDTH FILE > OUTPUT_FILESaves the reformatted text separately without changing the source.

Use a Safe File-Update Workflow

Running fmt with a file argument does not inherently edit that file in place. It writes the result to standard output. This behavior lets you inspect the result before changing the original.

  1. Write the formatted output to a new file.
  2. Review the generated file with a viewer or text editor.
  3. Replace the original only if the result is correct.
fmt -w 72 draft.txt > draft-formatted.txt
less draft-formatted.txt

After reviewing it, you can replace the source deliberately if appropriate:

mv draft-formatted.txt draft.txt

Suitable and Unsuitable Input

fmt treats input as reflowable text. It does not understand the semantic structure of markup or data formats.

Input typeUse fmt?Reason
Plain prose paragraphsYesThis is the intended use: words can be joined and wrapped at a selected width.
Mixed paragraph text with blank linesYes, selectivelyParagraphs can be reflowed independently when blank lines mark their boundaries.
Program source codeNoReflowing can change significant whitespace, comments, strings, or layout conventions.
Column-aligned tablesNoWrapping lines can destroy spacing and column alignment.
Structured configuration or data filesNofmt does not parse formats such as configuration syntax, CSV, JSON, or markup.

Use fmt only on prose sections of a document. Exclude code blocks, preformatted examples, aligned columns, and machine-readable sections.

Troubleshooting

The original file did not change

Cause: fmt writes its result to standard output rather than automatically overwriting the input file.

Resolution: Redirect the output to a new file, review it, and replace the original only when appropriate.

fmt notes.txt > notes-formatted.txt

Lines are not exactly the requested width

Cause: fmt wraps at word boundaries and normally avoids splitting words.

Resolution: Treat the selected width as a maximum target. Try a different width if the visual result does not suit the document.

A table or code block was damaged

Cause: fmt treats the input as prose and does not preserve column layout or programming syntax.

Resolution: Apply it only to ordinary prose and exclude preformatted, tabular, or structured content.

Separate paragraphs were combined

Cause: The input did not contain a clear blank-line boundary between paragraph blocks.

Resolution: Add or retain a blank line between paragraphs before formatting.

The command is not found

Check whether the command is available:

command -v fmt

If this produces no path, the system may lack the package that provides standard text utilities, or the command may not be available in the current environment. Install the platform's standard text-utilities package if needed.

Key Points

  • fmt reflows ordinary prose paragraphs into more uniform lines.
  • Blank lines generally separate paragraphs and are preserved as boundaries.
  • The default output width is 75 characters.
  • Use -w to select another maximum width, such as fmt -w 60 notes.txt.
  • File arguments do not automatically edit files; output normally goes to standard output.
  • Use > to save output separately, review it, and then replace the source if needed.
  • Do not use fmt on code, aligned tables, preformatted blocks, or structured data.