Reflow Text Predictably with GNU fmt

fmt reflows a plain-text file to a chosen line width without touching the source file itself. It can also split overlong lines and format quoted text, and the examples use GNU fmt 9.4 from the Ubuntu coreutils package installed on this machine. Allow about 10 minutes if you already have a file to test on.

Before you start

You need a shell and the fmt command; no elevated privileges are needed. fmt reads input and writes formatted text to standard output, so the basic command never changes the input file itself. Still redirect to a new path while you are learning the result: a shell redirection such as > notes.txt can truncate an existing file before fmt even runs, which is an easy way to lose data by accident.

Check the installed version first:

fmt --version

On the reference system, the line that matters is:

fmt (GNU coreutils) 9.4

The option names and defaults in this guide match that installed version.

Checkpoint: fmt is installed and you know which file will act as the input.

1. Reflow a paragraph to the default width

Create or pick a plain-text file. Here draft.txt holds one paragraph, separated from the next by a blank line. Run fmt with no formatting option:

fmt draft.txt

fmt writes the reformatted paragraph straight to the terminal. GNU fmt defaults to a maximum width of 75 columns, and it normally refills the paragraph, so existing line breaks inside it are not treated as permanent boundaries.

To keep the result, redirect it to a new file:

fmt draft.txt > draft-formatted.txt

Compare the two files before replacing anything:

diff -u draft.txt draft-formatted.txt

Recovery: if the diff is not what you wanted, just discard the new file and keep draft.txt. Neither command touched the input.

2. Set a narrower or wider width

Use --width, or its short form -w, followed by a number. That number is the maximum line width, not a target every line has to reach:

fmt --width=40 draft.txt
fmt -w 100 draft.txt

The abbreviated form -40 also works:

fmt -40 draft.txt

Narrower widths suit email, terminals and narrow documentation columns; wider ones cut down on wrapping but can be awkward to read. If you want fmt to aim shorter than the maximum, use --goal, which defaults to 93 percent of the selected width:

fmt --width=60 --goal=50 draft.txt

The goal only nudges where fmt tries to end lines; it never allows a line longer than the width. Verify with a line-numbered view if the boundary actually matters:

fmt --width=60 draft.txt | awk '{ print length($0), $0 }'

Every reported length should be 60 or less, aside from unusual handling of characters whose display width differs from their byte or character count.

Checkpoint: you can choose the maximum width and have actually verified the result, instead of judging it from a wrapped terminal window.

3. Preserve line boundaries when you only need splitting

Reflowing is not always what you want. A file may have deliberately arranged lines, or you may need to break long lines while keeping shorter ones separate. Use --split-only, or -s:

fmt --split-only --width=72 input.txt > split.txt

This splits lines longer than the selected width without refilling adjacent lines into a new paragraph. It suits line-oriented data with the occasional overlong record, but it does not understand every data format, so inspect the output before you use it in a script or commit it anywhere.

4. Reflow quoted or prefixed text

For text where every line starts with the same marker, use --prefix:

fmt --prefix='> ' --width=72 email-quote.txt > email-quote-formatted.txt

fmt only processes lines beginning with that exact prefix, and reattaches the prefix once wrapped. This suits plain-text quotations starting with > . The prefix counts towards the line width, so the quoted content gets fewer columns to work with.

Do not assume this formats arbitrary mixed email content correctly. Test a copy first, especially with unquoted lines, nested quote markers or signatures in the mix. A line that does not begin with the requested prefix is simply not touched by prefix mode.

5. Handle spacing and special layouts

By default, fmt preserves the input's spacing style as far as its reflow rules allow. --uniform-spacing, or -u, forces one space between words and two after sentences:

fmt --uniform-spacing --width=72 draft.txt > spaced.txt

Use this only when changing spacing is genuinely acceptable, since it can alter intentional extra spaces.

Two more options suit indented text. --crown-margin preserves the indentation of the first two lines. --tagged-paragraph treats the first line's indentation as different from the second. Both depend entirely on the actual indentation in your file, so start with a small copy and inspect it rather than combining them blindly.

6. Read from standard input

With no file operand, or with a file operand of -, fmt reads standard input, which makes it useful in a pipeline:

printf '%s\n' 'A long paragraph that needs to be wrapped for a terminal or a plain text message.' | fmt --width=40

For a reusable result, make the destination explicit:

printf '%s\n' 'PASTE YOUR TEXT HERE' | fmt --width=72 > formatted.txt

Replace the placeholder before running it. If a pipeline produces something unexpected, remove the destination file only after confirming it is the one you just created, then rerun with the input kept separately. fmt has no undo database: recovery means keeping the original input, or restoring it from version control or a backup.

Common traps

Done means