Home > Text Processing & Search > fmt

fmt: Format Text Paragraphs

The fmt command is used to reformat paragraphs of text files or standard input, adjusting line breaks to fit a specified width. It is particularly useful for improving the readability of emails, code comments, and plain text documents.

Overview

fmt optimizes the line length of text paragraphs to enhance readability. By default, it wraps lines to a width of 75 characters, but this can be adjusted using the -w option.

Key Features

  • Reformats text paragraph line breaks
  • Outputs to a specified width
  • Processes standard input and files
  • Offers various formatting options

Key Options

Formatting

Generated command:

Try combining the commands.

Description:

`fmt` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Basic Formatting (75-character width)

echo "This is a very long sentence. The fmt command can be used to reformat the line breaks of this sentence. It helps improve readability." | fmt

Formats text from standard input to the default width (75 characters).

Formatting to a Specific Width (e.g., 40 characters)

echo "This is a very long sentence. The fmt command can be used to reformat the line breaks of this sentence. It helps improve readability." | fmt -w 40

Uses the -w option to set the maximum width of output lines to 40 characters.

Formatting File Content

printf "This is a very long line that needs to be formatted. It will be wrapped to the default width.\n" > example.txt
fmt example.txt
rm example.txt

Reads the content of a file, formats it, and outputs the result to standard output.

Splitting Long Lines Without Joining Short Ones

echo -e "Short line.\nThis is a very long sentence. The fmt command can be used to reformat the line breaks of this sentence. It helps improve readability." | fmt -s

Uses the -s option to split only long lines, keeping short lines as they are.

Formatting Comment Blocks

echo -e "# This is a very long comment line that needs to be formatted to a specific width.\n# It should only affect lines starting with a hash symbol." | fmt -w 50 -p "#"

Uses the -p option to format only lines starting with a specific prefix.

Tips & Notes

fmt is primarily optimized for formatting paragraphs in plain text files. It may not be suitable for code or structured data.

Usage Tips

  • Automatic line wrapping for long lines when writing emails
  • Aligning paragraphs in README files or documents
  • Using pipes (|) to combine with other commands

Notes

fmt operates on paragraphs, so text not separated by blank lines may be treated as a single long paragraph. For formatting code or script files, it is recommended to use dedicated tools like `clang-format` or `prettier`.


Same category commands