← 返回命令列表

Linux command

fmt 命令

文件

复制后可按需替换文件名、目录或参数。

常用示例

Reformat a text file

fmt [path/to/file.txt]

Reformat to a specific width

fmt -w [60] [path/to/file.txt]

Reformat text from stdin

echo "[long text here]" | fmt

Preserve paragraph indentation

fmt -p [prefix] [file.txt]

Split long lines only

fmt -s [file.txt]

Set goal and maximum width

fmt -g [70] -w [80] [file.txt]

Uniform spacing

fmt -u [file.txt]

Format multiple files

fmt [file1.txt] [file2.txt]

说明

fmt is a simple text formatter that rewraps paragraphs to fit within a specified line width. It reads text from files or stdin, reformats it, and outputs to stdout. The tool intelligently handles paragraphs: consecutive non-blank lines are treated as a single paragraph and reflowed together. Blank lines, indentation changes, and other formatting cues mark paragraph boundaries. The formatting algorithm aims for the goal width while staying under the maximum width. This produces more natural-looking text than simply breaking at exactly the maximum width. Words are never hyphenated or broken. Common uses include formatting text for emails, documentation, code comments, and improving readability of text files. The -s option is useful when you only want to break overly long lines without affecting properly formatted text. Unlike more complex formatters, fmt handles plain text only. It doesn't understand markup, code blocks, or special formatting. It's designed for simple prose paragraphs.

参数

-w, --width _n_
Maximum line width (default: 75).
-g, --goal _n_
Goal width (optimal line length, default: 93% of width).
-s, --split-only
Split long lines but do not join short ones.
-u, --uniform-spacing
One space between words, two after sentences.
-c, --crown-margin
Preserve first two lines' indentation.
-p, --prefix _string_
Only reformat lines starting with prefix.
-t, --tagged-paragraph
Preserve indentation of first line.
--help
Display help and exit.
--version
Display version and exit.

FAQ

What is the fmt command used for?

fmt is a simple text formatter that rewraps paragraphs to fit within a specified line width. It reads text from files or stdin, reformats it, and outputs to stdout. The tool intelligently handles paragraphs: consecutive non-blank lines are treated as a single paragraph and reflowed together. Blank lines, indentation changes, and other formatting cues mark paragraph boundaries. The formatting algorithm aims for the goal width while staying under the maximum width. This produces more natural-looking text than simply breaking at exactly the maximum width. Words are never hyphenated or broken. Common uses include formatting text for emails, documentation, code comments, and improving readability of text files. The -s option is useful when you only want to break overly long lines without affecting properly formatted text. Unlike more complex formatters, fmt handles plain text only. It doesn't understand markup, code blocks, or special formatting. It's designed for simple prose paragraphs.

How do I run a basic fmt example?

Run `fmt [path/to/file.txt]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -w, --width _n_ do in fmt?

Maximum line width (default: 75).