Text and processing

The fmt command

Reflows paragraphs to a fixed width, joining short lines and splitting long ones.

Syntax

fmt [-w N] [opciones] [archivo...]Try it in the terminal →

Most useful options

-w NMaximum line width. The default is 75 columns. As a shorthand, fmt -10 is the same as fmt -w 10
-g NGoal width each line tries to reach. The default is 93% of the maximum width
-sSplits long lines without refilling the short ones
-uUses one space between words and two after a sentence end
-cKeeps the indentation of the first two lines of each paragraph
-p CADENAReformats only the lines that start with that string and puts the prefix back

Copy-ready examples

$ printf 'uno dos tres cuatro\n' | fmt -w 10reflows to 10 columns.

Example output

uno dos
tres
cuatro
$ printf 'a\nb\n\nc d\ne\n' | fmt -w 10joins the lines of each paragraph and keeps the blank line that separates them.

Example output

a b

c d e
$ fmt -w 60 notas.txt > notas-60.txtreflows a file to 60 columns and saves the result in another file. The original does not change.
$ fmt -s -w 40 notas.txtonly splits the lines longer than 40 columns and leaves the short ones as they are.
$ fmt -p '# ' script.shreflows only the comments that start with '# ', leaving the rest of the code alone.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
fmt: invalid width: 'x'The -w value is not a number. You typed a letter or a word where the width goes.Use a whole number, for example fmt -w 72 notas.txt.

Typical mistakes

  • A blank line or an indentation change separates paragraphs, and fmt does not merge them. Two touching blocks with the same indentation count as one paragraph.
  • fmt writes the result to standard output and does not touch the original. To keep it, redirect with > to another file.
  • With -u, several spaces in a row between words become one: the text changes even if the width does not.

Differences from similar commands

  • fold: cuts each line at a fixed width, without joining paragraphs or spreading words. fmt does refill the paragraph.
  • column: arranges text into columns or tables. fmt only spreads text within a width.

Related commands

FAQ

Does fmt change the original file?

No. It writes to standard output. If you want to keep the result, redirect it with > to another name.

How do I reflow only the comments in some code?

With -p and the comment prefix, for example -p '# ' for shell comments. Lines without that prefix are left as they are.

What width does it use by default?

75 columns. Use -w to change the maximum and -g for the goal it tries to reach.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course