Text processing
fmt
Reflow text into paragraphs of a given width, joining short lines and wrapping long ones.
fmt [OPTION...] [FILE...]
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
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
# 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
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
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
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
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 '#'
# 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
fmtjoins consecutive non-blank lines. On lists, code or tables usefmt -s, or don't usefmtat all.The default width is 75, not 80. Pass
-wexplicitly when the width matters.fmtis 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
fmthas a different option set: the width is given asfmt 72or-w 72, and GNU options such as-p,-uand-sbehave 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.
| Option | What it does |
|---|---|
-w, --width WIDTH | Maximum line width (75 by default). |
-g, --goal WIDTH | Preferred line width, a little less than the maximum. |
-s, --split-only | Split long lines but never join short ones. |
-u, --uniform-spacing | One space between words, two after sentences. |
-p, --prefix STRING | Only reformat lines starting with STRING, and keep the prefix. |
-c, --crown-margin | Keep the indentation of the first two lines of each paragraph. |
-t, --tagged-paragraph | Treat a first line indented differently from the second as a hanging indent. |