opscape

Text processing

fmt

Reflow text into paragraphs of a given width, joining short lines and wrapping long ones.

fmt [OPTION...] [FILE...]

7 of 7 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: fmt(1)

fmt is a simple paragraph formatter. It treats runs of lines separated by blank lines as paragraphs, joins each paragraph's lines together and re-wraps them so no line exceeds the width (75 columns by default, set with -w). Indentation is kept, and GNU fmt tries to break lines in visually balanced places rather than greedily.

It is the right tool for commit messages, plain-text emails and README paragraphs; editors like Vim can even pipe a selection through it. Because it joins lines, it will happily merge a list into a paragraph: use -s to only split long lines, or keep blank lines between items.

Examples

Rewrap a paragraph to a width

printf 'This line is short.\nThis one is a little longer than the first one, and it keeps going for a while.\nEnd.\n' | fmt -w 40
explain
output
This line is short.  This one is a
little longer than the first one,
and it keeps going for a while.  End.

The three input lines form one paragraph, so fmt joins them and breaks the text again into lines of at most 40 columns. The original line breaks are gone, and where a line ended in a period fmt puts two spaces after it (short. This), the old typewriter convention.

Blank lines separate paragraphs

fmt -w 30 README.md
explain
output
# demo

A small sample project
used by every example on
this site.  It has logs,
CSV and JSON data, a little
source code and some photos.

## Layout

- data/  CSV, JSON and text
files - src/   source code -
logs/  application logs

Each block between blank lines is reflowed on its own; the blank lines stay. The heading # demo is a one-line paragraph, and the two list items are joined because no blank line separates them.

Trap: lists get merged into one paragraph

fmt -w 40 notes.txt
explain
output
Meeting notes, March - Move backups
to the new server - Rotate the API
key before Friday - Check disk usage
on web01 - TODO: write the deploy
script Ask Priya about the error rate
on web02.

notes.txt has no blank lines, so fmt sees one paragraph and runs the list items into each other. The - markers end up in the middle of lines.

Wrap long lines without joining short ones

fmt -s -w 20 notes.txt
explain
output
Meeting notes,
March
- Move backups to
the new server
- Rotate the API
key before Friday
- Check disk usage
on web01
- TODO: write the
deploy script
Ask Priya about
the error rate
on web02.

-s (split only) breaks lines that are too long but never joins lines, so each list item starts on its own line and only the long ones wrap.

Normalize spacing

echo 'one   two.  three     four' | fmt -u
explain
output
one two.  three four

-u squeezes runs of spaces to one between words and two after a sentence end, the typewriter convention fmt uses: one two. three four.

Indentation is preserved

printf '    Indented text that is long enough to need wrapping at thirty columns.\n' | fmt -w 30
explain
output
    Indented text that is
    long enough to need
    wrapping at thirty
    columns.

The continuation lines get the same 4-space indent as the first line, so quoted or nested text keeps its shape.

Reflow comment lines, keeping the prefix

printf '# This comment was written as one very long line that nobody wrapped.\n' | fmt -w 30 -p '#'
explain
output
# This comment was written
# as one very long line that
# nobody wrapped.

-p '#' reformats only lines that start with # and puts the prefix back on every output line, so a long shell comment becomes a tidy block.

Common pitfalls

  • fmt joins consecutive non-blank lines. On lists, code or tables use fmt -s, or don't use fmt at all.

  • The default width is 75, not 80. Pass -w explicitly when the width matters.

  • fmt is not a typesetter: it does not hyphenate, justify or understand Markdown. Lines starting with # or - are just words to it.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • BSD/macOS fmt has a different option set: the width is given as fmt 72 or -w 72, and GNU options such as -p, -u and -s behave differently or don't exist. Check your system's man page.

Options worth knowing

A selection, described in our own words. For the full list, see fmt(1) or fmt --help.

OptionWhat it does
-w, --width WIDTHMaximum line width (75 by default).
-g, --goal WIDTHPreferred line width, a little less than the maximum.
-s, --split-onlySplit long lines but never join short ones.
-u, --uniform-spacingOne space between words, two after sentences.
-p, --prefix STRINGOnly reformat lines starting with STRING, and keep the prefix.
-c, --crown-marginKeep the indentation of the first two lines of each paragraph.
-t, --tagged-paragraphTreat a first line indented differently from the second as a hanging indent.

Related