keyboard-keys Reformat text on the command line

Here's how I use fmt to reformat text to write plain text files or to quote text.

When I write documentation for a project, I often write the Readme file and Install instructions in plain text. I don't need to use markup languages like HTML or Markdown to describe what a project does or how to compile it. But maintaining this documentation can be a pain. If I need to update the middle of a sentence in my Readme text file, I need to reformat the text so I don't end up with a really long or short line in the middle of my other text that's otherwise formatted to 75 columns. Some editors include a feature that will automatically reformat text to fill paragraphs, but not all do. That's where the fmt command comes to the rescue. You can find fmt on Linux, FreeBSD, and other Unix-like systems.

Simple text formatting

The fmt command is a trivial text formatter; it collects words and fills paragraphs, but doesn't apply any other text styling such as italics or bold. It's all just plain text. With fmt, you can quickly adjust text so it's easier to read. Let's say I start with this sample text:

$ cat readme.txt 
The fmt command is a trivial text formatter.
Here's how I use it to reformat text to write plain text files
or to quote text.

In this sample file, lines have different lengths, and they are broken up in an odd way. You might have similar odd line breaks if you make lots of changes to a plain text file. To reformat this text, you can use the fmt command to fill the lines of the paragraph to a uniform length:

$ fmt readme.txt 
The fmt command is a trivial text formatter.  Here's how I use it to
reformat text to write plain text files or to quote text.

By default, fmt will format text to 75 columns wide, but you can change that with the -w or --width option:

$ fmt -w 60 readme.txt 
The fmt command is a trivial text formatter.  Here's how
I use it to reformat text to write plain text files or to
quote text.
$ fmt --width=60 readme.txt 
The fmt command is a trivial text formatter.  Here's how
I use it to reformat text to write plain text files or to
quote text.

Reformatting quoted text

When writing documentation in plain text, I sometimes need to include or quote other text inside my document. The fmt command can recognize text that is quoted, such as lines that begin with the > character.

Here's a simple example. Let's say I needed to include a line from Lorem Ipsum, but it was broken up in an odd way:

$ cat quote.txt 
> Lorem ipsum dolor sit amet,
> consectetur adipiscing elit,
> sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

To fix this, I use the -p or --prefix option to tell fmt what character to use as a "prefix" before each line.

$ fmt -p '>' quote.txt 
> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
> tempor incididunt ut labore et dolore magna aliqua.
$ fmt --prefix='>' quote.txt 
> Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod
> tempor incididunt ut labore et dolore magna aliqua.

The fmt command is a very simple text formatter, but it can do lots of useful things that help in writing and updating documentation in plain text. Explore the other options such as -c or --crown-margin to match the indentation of the first two lines of a paragraph, such as bullet lists. Also try -t or --tagged-paragraph to preserve the indentation of the first line in a paragraph, like indented paragraphs. And the -u or --uniform-spacing option to use one space between words and two spaces between sentences.