fmt

text processinglinux
The fmt command is one of the most frequently used commands in Linux/Unix-like operating systems. fmt The fmt command is a simple text formatter that rewraps text paragraphs to fit within specified width limits. It's useful for formatting documentation, README files, and other plain text content to improve readability.

Quick Reference

Command Name:

fmt

Category:

text processing

Platform:

linux

Basic Usage:

fmt [options] [arguments]

Common Use Cases

  • 1

    Text formatting

    Format text data for optimal readability

  • 2

    Report generation

    Generate reports with optimal line wrapping

  • 3

    Scripting

    Use in shell scripts to format text data programmatically

  • 4

    Text processing

    Manipulate text data in pipelines and scripts

Syntax

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

Options

Option Description
-c, --crown-margin Preserve indentation of first two lines in each paragraph
-p PREFIX, --prefix=PREFIX Only reformat lines beginning with PREFIX, reattaching PREFIX to reformatted lines
-s, --split-only Split long lines but don't refill (reformat) paragraphs
-t, --tagged-paragraph Indentation of first line different from second line
-u, --uniform-spacing One space between words, two after sentences
-w WIDTH, --width=WIDTH Set maximum line width to WIDTH (default 75 characters)
--help Display help information and exit
--version Output version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the fmt command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

fmt filename.txt

Format the text in the file with default settings, wrapping lines to 75 characters and preserving paragraph breaks. The result is displayed on standard output.

fmt -w 60 filename.txt

Format the text with a line width of 60 characters instead of the default 75. This is useful when preparing text for narrower displays or specific layout requirements.

fmt -w 60 filename.txt > formatted.txt

Format the text with a width of 60 characters and save the result to a new file instead of displaying it on screen.

cat filename.txt | fmt -w 80

Read input from a pipe instead of a file, format it with a width of 80 characters. This demonstrates how fmt can work within command pipelines.

Advanced Examples:

fmt -s filename.txt

Split long lines but don't refill paragraphs. This preserves the original paragraph structure while ensuring no line exceeds the width limit.

fmt -u filename.txt

Use uniform spacing (one space between words, two after sentences). This produces more consistent, typographically correct text formatting.

fmt -c filename.txt

Format the text while preserving indentation in the first two lines of a paragraph. This is useful for maintaining formatting in documents where indentation indicates new paragraphs.

fmt -p '# ' filename.txt

Format text while preserving paragraphs that begin with the prefix '# '. This is particularly useful for formatting documents that contain comments or special markers.

find . -name "*.md" -exec fmt -w 80 -s {} \; -exec mv {} {}.new \; -exec rename "s/.new$//" {}*.new \;

Find all Markdown files in the current directory and its subdirectories, format them to 80 characters without refilling paragraphs, and save the changes back to the original files.

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

What Fmt Does:

  • Reformats text by wrapping lines to specified width (default 75 characters)
  • Preserves paragraph breaks (blank lines between paragraphs)
  • Can preserve indentation and special line prefixes
  • Simplifies text formatting for documentation and plain text files
  • Can standardize spacing between words and sentences
  • Works with files or standard input/output for pipeline integration

Common Use Cases:

  • Formatting README files and documentation for better readability
  • Preparing text for display in terminals or consoles with specific width constraints
  • Standardizing text formatting across multiple files
  • Cleaning up text with inconsistent line wrapping
  • Adapting text for different display media (wider or narrower)
  • Formatting email messages or other plain text content

Paragraph Handling:

  • By default, fmt considers a paragraph to be separated by one or more blank lines
  • The -c option (crown margin) preserves indentation of the first two lines in a paragraph
  • The -t option handles paragraphs where the first line has different indentation than subsequent lines
  • The -p option allows special handling of lines that begin with a specified prefix
  • The -s option only breaks long lines without reformatting entire paragraphs

Text Formatting Rules:

  • Words are normally separated by a single space
  • With the -u option, sentences are separated by two spaces (typographical convention)
  • Default line width is 75 characters, which works well for most text displays
  • Line breaks in the input are treated as whitespace for reformatting purposes
  • Consecutive blank lines are preserved to maintain paragraph separation
  • Indentation and line prefixes can be preserved with appropriate options

Related Commands:

  • pr - Format text files for printing (more features but more complex)
  • fold - Wrap each line to a specified width (simpler, doesn't reformat paragraphs)
  • expand - Convert tabs to spaces
  • unexpand - Convert spaces to tabs
  • sed - Stream editor for filtering and transforming text
  • awk - Pattern scanning and text processing language
  • groff - More sophisticated document formatting system

Tips & Tricks

1

Use the -w width option to specify the output width

2

Use the -u option to unfold paragraphs

3

Use the -s option to split long lines

4

Use the -g margin option to specify the right margin

5

Use the -h option to display help

Common Use Cases

Text formatting

Format text data for optimal readability

Report generation

Generate reports with optimal line wrapping

Scripting

Use in shell scripts to format text data programmatically

Text processing

Manipulate text data in pipelines and scripts

Data presentation

Present data in a structured and readable format

Related Commands

These commands are frequently used alongside fmt or serve similar purposes:

Use Cases

1

Text formatting

Format text data for optimal readability

2

Report generation

Generate reports with optimal line wrapping

3

Scripting

Use in shell scripts to format text data programmatically

4

Text processing

Manipulate text data in pipelines and scripts

5

Data presentation

Present data in a structured and readable format

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the fmt command works in different scenarios.

$ fmt
View All Commands