Skip to main content

The `fmt` Command in Linux

The fmt command in Linux is a simple text formatter that reflows paragraph text to a specified width. It's mainly used to make a text file more readable in a terminal window or to format the content before piping it to another command. Unlike the fold command which simply wraps text at a given width, fmt tries to balance line lengths and does not break words.

Syntax

The basic syntax of the fmt command is:

fmt [OPTIONS] [FILE]...

If no FILE is specified, or when FILE is -, fmt reads from standard input.

Options

Here's a table of common fmt options:

OptionShorthandDescription
--width=WIDTH-wSet output line width to WIDTH (default is 75 columns).
--goal=GOAL-gSet goal line width to GOAL (default is 93% of WIDTH).
--uniform-spacing-uUse one space between words and two after sentences.
--split-only-sSplit long lines, but do not refill.
--tagged-paragraph-tIndentation signifies a paragraph (for tagged text).
--crown-margin-cPreserve indentation of first two lines within a paragraph.
--prefix=STRING-pReformat only lines beginning with STRING.
--helpDisplay help information and exit.
--versionOutput version information and exit.

Creating a Sample Text File with vim

To create a text file using vim, open the terminal and type:

vim sampletext.txt

Once in vim, press i to switch to insert mode and enter the following text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

To save and exit, press Esc, then type :wq and press Enter.

Examples of Using fmt

Example 1: Basic Formatting

To format text with default settings:

fmt sampletext.txt

This will output the text with each line wrapped at 75 columns by default.

Example 2: Specifying Width

To format the text to a width of 50 columns:

fmt -w 50 sampletext.txt

Example 3: Uniform Spacing

For uniform spacing between words and sentences:

fmt -u sampletext.txt

Example 4: Crown Margin

To maintain the indentation of the first two lines in each paragraph:

fmt -c sampletext.txt

This is particularly useful for emails or documents with quoted text.

Example 5: Tagged Paragraph

If you're working with tagged text, use the -t option:

fmt -t sampletext.txt

Combining fmt with Other Commands

fmt can be combined with other Unix commands. For instance, to paginate the formatted output:

fmt -w 50 sampletext.txt | less

Tips for Using fmt

  • fmt is best used for reformatting paragraph text rather than code or non-prose material.
  • For scripting, it's useful to pipe content through fmt to ensure consistent formatting before further processing.
  • fmt doesn't modify the input file unless you redirect the output back into the file using something like fmt sampletext.txt > sampletext.txt (be cautious with this, as it's safer to write to a new file to avoid data loss).

The fmt command is one of the many text processing utilities available on Linux that can help in managing and reformatting text files for better readability and processing.

What Can You Do Next 🙏😊

If you liked the article, consider subscribing to Cloudaffle, my YouTube Channel, where I keep posting in-depth tutorials and all edutainment stuff for software developers.

YouTube @cloudaffle