Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux fmt command

Reference for the Linux fmt command: reflow paragraphs to a target width, join short lines, keep indentation, split-only mode and prefix handling.

fmt is a simple text formatter. It reads text, treats blank lines as paragraph separators, and rewrites each paragraph so that the lines are filled up to a target width. Short lines are joined, long lines are broken, and words are never split.

It is the tool for tidying README text, comments and plain-text email. Compared with fold, which only cuts, fmt rebuilds the paragraph, and it keeps the indentation of each paragraph's first two lines.

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

Try it

Bash Reflowing a paragraph

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Split only, prefixes and indentation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
fmt [-cstu] [-w WIDTH] [-g GOAL] [-p PREFIX] [FILE]...
Option Description
-w N, --width=N Maximum line width (default 75).
-g N, --goal=N Target width that lines are filled towards (default 93% of the width).
-s Split long lines only; never join short lines.
-c Crown margin: keep the indentation of the first two lines of each paragraph.
-t Tagged paragraph: the first line's indentation differs from the second.
-u Uniform spacing: one space between words, two after a sentence end.
-p PREFIX Only reformat lines that start with PREFIX, keeping that prefix on output.

Returns: 0 on success, non-zero on error.

Usage notes

  • Blank lines separate paragraphs and are preserved. Lines that differ in indentation also start new paragraphs.
  • Goal width: fmt aims slightly shorter than -w so lines look even. If you pass -w 40, many lines end around 37 characters. Set -g to reduce this.
  • Sentence ends (a period followed by two spaces, or at a line end) get extra spacing, which can differ from what you expect in modern single-space text. -u normalizes this.
  • Long words are left alone even if wider than the limit.
  • Code and tables: do not run fmt on source code, Markdown tables or lists; it reflows everything it considers a paragraph. In an editor, filter only selected text.
  • Use -s when you only need to make sure lines fit, for example fmt -s -w 72 in a commit-message check.
  • -p is perfect for comment blocks: it rewraps only lines beginning with the comment character and leaves code unchanged.
  • Multibyte text is handled by characters in a UTF-8 locale, but wide East Asian characters are not measured by display width.