How to Use the fmt Command to Format Text in Linux

The Broken Text Problem

If you have ever opened a raw text file (like an email draft or a README document) in the Linux terminal using a basic tool like cat, you have likely experienced the broken text problem. If the original author typed a massive, 500-word paragraph without ever pressing the “Enter” key, the terminal will attempt to display that single line of text by violently stretching it across your entire screen.

If the line is longer than your monitor is wide, the terminal will simply chop the words in half at the right margin and violently shove the rest of the sentence onto the next line. This completely destroys the readability of the document. If you try to open the file in a code editor like nano to fix it, you have to manually press the “Enter” key at the end of every single line, which takes forever.

To mathematically force a chaotic block of text to conform to a strict, readable width limit, you must use the fmt (Format) command.

Step 1: The Basic Format

The fmt command is a structural text editor. It scans a file, ignores where the original author pressed the Enter key, and rebuilds the paragraphs from scratch, injecting perfectly placed line breaks to ensure the text looks like a professionally formatted book.

Assume you have a file named broken_essay.txt containing massive, unreadable lines of text.

fmt broken_essay.txt

The terminal will instantly output the text, perfectly formatted. By default, the fmt command restricts every single line to a maximum width of exactly 75 characters. If a word threatens to cross the 75-character limit, fmt smoothly pushes the entire word down to the next line, guaranteeing that words are never chopped in half.

Step 2: Customizing the Column Width

The 75-character default is excellent for reading on a standard monitor, but what if you are formatting text that will be printed on a small receipt printer, or displayed on a tiny mobile device screen?

You can force fmt to use any mathematical width you desire by using the -w (width) flag.

fmt -w 40 broken_essay.txt

The terminal will instantly squash the text into a tall, skinny column, exactly 40 characters wide. Conversely, if you are reading on an ultra-wide monitor, you can expand it to 120 characters to utilize the full width of your screen.

Remember, like most Linux utilities, fmt only alters the text on the screen. It does not overwrite the original file. To permanently save your beautiful new formatting, you must redirect the output into a new file.

fmt -w 75 broken_essay.txt > fixed_essay.txt

Step 3: Preserving Intentional Indentation

If you are running the fmt command on a computer program’s source code (like a Python script with massive comments), you must be incredibly careful. In programming, indentation (the spaces at the beginning of a line) is mathematically critical. If fmt accidentally destroys the indentation, the program will crash.

To tell Linux to perfectly preserve the original spacing at the beginning of every single line while only wrapping the text at the right margin, you must use the -s (split only) flag.

fmt -s -w 75 code_comments.txt

This command acts with surgical precision. It will wrap the long sentences without ever joining short lines together or altering the left-hand margin.

Step 4: The Uniform Spacing Trick

If the original author of the text file had a terrible habit of typing two spaces after every period, or inserting random clusters of spaces between words, you can use fmt to ruthlessly standardize the spacing.

Use the -u (uniform) flag.

fmt -u -w 75 broken_essay.txt

This command destroys all chaotic spacing. It mathematically enforces exactly one space between words, and exactly two spaces after a sentence-ending period. By mastering the fmt command, you transform chaotic data dumps into highly readable, professional documents.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.