Linux online course

Reformat Paragraphs with the Linux fmt Command

Learn how to use Linux fmt to reflow prose paragraphs, choose a line width, preserve paragraph breaks, and safely save formatted output.

The Linux fmt command reformats plain-text paragraphs by wrapping prose to a selected line width. It is useful when a text file contains irregular line lengths, long manually entered lines, or paragraphs that are difficult to read in a terminal or editor.

Paragraph reformatting means rewrapping prose so that its lines have a more consistent and readable length. Unlike a simple character cutter, fmt normally wraps at word boundaries and balances output lines where possible. The requested width is therefore a target or maximum wrapping width, not a promise that every line will contain exactly that many characters.

Prerequisites and key terms

You should be comfortable running commands in a terminal, identifying files, editing basic text files, and using shell output redirection. See Linux command-line topics for related fundamentals.

TermMeaning
fmtA text-formatting utility that reflows paragraphs to a selected line width.
Line widthThe target maximum number of characters used when wrapping output lines.
Input fileThe text file supplied to fmt for processing.
Standard outputThe default output stream, usually displayed in the terminal.
RedirectionShell syntax that sends command output to a file instead of the terminal.

Basic fmt syntax

fmt [options] [file]

You can provide a file name as the input. If no file is supplied, fmt can read text from standard input. By default, the reformatted text is written to standard output, so running fmt does not change the input file.

fmt paragraph.txt

This command reads paragraph.txt and displays the reformatted result in the terminal. The original file remains unchanged unless you redirect the output or use another method to replace it.

Default line width

When no width is specified, fmt uses a default target width of 75 characters. The effective result also depends on the words, existing paragraph structure, and options used. Words are generally kept intact, and the command may produce shorter lines when placing another word would exceed the target.

Reformat an uneven paragraph

Suppose paragraph.txt contains one paragraph whose lines were entered at inconsistent lengths:

Linux provides many small utilities that each perform a focused task. Some text files have lines that are very long, while others contain short lines because the author pressed Enter at arbitrary places. This makes ordinary prose harder to read and edit.

Run fmt with its default width:

fmt paragraph.txt

The displayed output is rewrapped approximately like this:

Linux provides many small utilities that each perform a focused task. Some
text files have lines that are very long, while others contain short lines
because the author pressed Enter at arbitrary places. This makes ordinary
prose harder to read and edit.

The exact line endings depend on the input text and the implementation, but the result is generally more uniform than the original. fmt does not simply cut every input line at character 75; it combines and rearranges words within a paragraph.

Original text characteristicsfmt result
Uneven line lengthsLines are rewrapped toward a consistent target width.
Long manually entered linesProse is split at suitable word boundaries.
Blank lines between paragraphsBlank-line paragraph separation is generally retained.

Choose a custom width

Use the -w option followed by a number to choose a narrower or wider target:

fmt -w 50 paragraph.txt

This reformats the paragraph for a target width of 50 characters, which can be useful for a narrow terminal, email, or text display. A long option is also available on implementations that support it:

fmt --width=50 paragraph.txt
OptionMeaningExample
-w WIDTHSet the target output width.fmt -w 50 paragraph.txt
--width=WIDTHLong-form spelling for setting the width, where supported.fmt --width=50 paragraph.txt
file argumentRead and reformat the named input file.fmt paragraph.txt

Save the reformatted output

Because fmt writes to standard output, use the shell’s > redirection operator to save the result:

fmt -w 60 paragraph.txt > paragraph-formatted.txt

This leaves paragraph.txt intact and creates paragraph-formatted.txt with the rewrapped content. Writing to a separate file is safer because you can compare the files and recover the original if the result is unsuitable.

If you intentionally want to replace the original, first write to a temporary file and move it into place only after formatting succeeds:

fmt -w 60 paragraph.txt > paragraph.tmp && mv paragraph.tmp paragraph.txt

The && operator runs mv only when fmt and the redirection operation complete successfully. Use a temporary file name that does not conflict with another file in the same directory.

Paragraph boundaries and limitations

Blank lines generally separate paragraphs. Within a paragraph, existing line endings may be changed during reflow. Keep a blank line between paragraphs that must remain separate:

First paragraph contains several sentences that may be rewrapped together when fmt processes the text.

Second paragraph remains a separate block because a blank line separates it from the first.

fmt is intended for prose-like plain text. Do not run it over content whose spacing or line positions have a specific meaning, including:

  • Source code, where indentation and line structure may be significant.
  • Tables and column-aligned text.
  • Fixed-width records and data files.
  • Preformatted examples or text with deliberate manual line breaks.

Reflowing such content can damage alignment, alter deliberate breaks, or make the content invalid. Isolate only the prose paragraphs before formatting.

Troubleshooting

The source file did not change

This is expected when you run fmt paragraph.txt. The result goes to standard output rather than back into the input file. Redirect it to a separate file, or use the temporary-file-and-mv method shown above.

Lines are not exactly the requested width

fmt reflows words and aims for balanced lines. It does not normally split ordinary words just to fill an exact character count. Treat the width as a target or maximum wrapping width and choose a value appropriate for the destination.

Formatting or alignment was damaged

The input may contain code, columns, tables, fixed-width records, or intentional line breaks. Do not format those sections with fmt; process only ordinary prose.

Separate paragraphs were combined

Paragraph separation depends on blank lines in the input. Add a blank line between blocks that should remain distinct, then run fmt again.

Exam-relevant notes

  • The general syntax is fmt [options] [file].
  • The default target width is 75 characters.
  • -w WIDTH selects a custom width; --width=WIDTH is the long form where supported.
  • Output goes to standard output by default, so the input file is not changed automatically.
  • fmt reflows prose at word boundaries and does not guarantee exact-width lines.
  • Blank lines generally preserve paragraph boundaries.
  • Use redirection to save output, preferably to a separate file before replacing the source.

Quick reference

# Default 75-character target width
fmt paragraph.txt

# Narrower 50-character target width
fmt -w 50 paragraph.txt

# Long option form
fmt --width=50 paragraph.txt

# Save a formatted copy
fmt -w 60 paragraph.txt > paragraph-formatted.txt

# Safely replace the original after formatting
fmt -w 60 paragraph.txt > paragraph.tmp && mv paragraph.tmp paragraph.txt