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.
fmt [OPTION]... [FILE]...
Try it
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.
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
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:
fmtaims slightly shorter than-wso lines look even. If you pass-w 40, many lines end around 37 characters. Set-gto 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.
-unormalizes this. - Long words are left alone even if wider than the limit.
- Code and tables: do not run
fmton source code, Markdown tables or lists; it reflows everything it considers a paragraph. In an editor, filter only selected text. - Use
-swhen you only need to make sure lines fit, for examplefmt -s -w 72in a commit-message check. -pis 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.
Related
fold,cat,expand and unexpand,column- Lessons: Viewing text, Text processing recipes