• Skip to main content
  • Skip to after header navigation
  • Skip to site footer

Free-Mail

  • Events
  • Images
    • Instagram Goodness
  • Music
  • News
  • Software

How I use the Linux fmt command to format text

How I use the Linux fmt command to format text: How I use the Linux fmt command to format text Jim Hall Thu, 07/21/2022 – 03:00

Register or Login to like
Register or Login to like

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 Linux fmt command comes to the rescue.

More Linux resources
Linux commands cheat sheet
Advanced Linux commands cheat sheet
Free online course: RHEL technical overview
Linux networking cheat sheet
SELinux cheat sheet
Linux common commands cheat sheet
What are Linux containers?
Our latest Linux articles

Format text with Linux fmt

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 familiar sample text:

 $   cat trek.txt  Space: the final frontier. These are the voyages of the starship Enterprise. Its continuing mission: to explore strange new worlds. To seek out new life and new civilizations. To boldly go where no one has gone before!

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 trek.txt  Space: the final frontier. These are the voyages of the starship Enterprise. Its continuing mission: to explore strange new worlds. To seek out new life and new civilizations. To boldly go where no one has gone before!

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

 $   fmt -w 60 trek.txt  Space: the final frontier. These are the voyages of the starship Enterprise. Its continuing mission: to explore strange new worlds. To seek out new life and new civilizations. To boldly go where no one has gone before!

Format email replies with Linux fmt

I participate in an email list where we prefer plain text emails. That makes archiving emails on the list server much easier. But the reality is not everyone sends emails in plain text. And sometimes, when I reply to those emails as plain text, my email client puts an entire paragraph on one line. That makes it difficult to “quote” a reply in an email.

Here’s a simple example. When I’m replying to an email as plain text, my email client “quotes” the other person’s email by adding a > character before each line. For a short message, that might look like this:

 > I like the idea of the interim development builds.

A long line that doesn’t get “wrapped” properly will not display correctly in my plain text email reply, because it will be just one long line with a > character at the front, like this:

 > I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with.

To fix this, I bring up a terminal and copy and paste the quoted text into a new file. Then I use the -p or –prefix option to tell fmt what character to use as a “prefix” before each line.

 $   cat > email.txt > I like the idea of the interim development builds. This should be a great way to test new changes that everyone can experiment with. ^D $   fmt -p '>' email.txt > I like the idea of the interim development builds. This should be a > great way to test new changes that everyone can experiment with.

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.

The fmt command is a trivial text formatter. Here’s how I use to format text and email replies.

Typewriter keys
Image by:

Original photo by Bob Doran via Flickr. Modified by Rikki Endsley. CC BY-SA 2.0.

Linux

What to read next
Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.
Register or Login to post a comment.

: Opensource.com Jim Hall

Supporting Open Source.

Have you tried: Diving with Sharks?

Share this:

  • Share on Facebook (Opens in new window) Facebook
  • Share on X (Opens in new window) X
  • Share on Pinterest (Opens in new window) Pinterest
  • Email a link to a friend (Opens in new window) Email
  • More
  • Share on Tumblr (Opens in new window) Tumblr
  • Print (Opens in new window) Print
  • Share on LinkedIn (Opens in new window) LinkedIn
  • Share on Reddit (Opens in new window) Reddit
Published on 21 July 2022 by Alan Category: SoftwareTag: command, format, Linux, Text

Gregor Townsend makes seven changes to Scotland’s line-up for Japan clash

South Africa in the news: Gregor Townsend makes seven changes to Scotland’s line-up for Japan clash: …

Read moreGregor Townsend makes seven changes to Scotland’s line-up for Japan clash

Strand von Tarifa IIII

A cool Strand Beach image: Strand von Tarifa IIII Image by Andreas Issleib (Photo: Andreas Issleib) …

Read moreStrand von Tarifa IIII

Soul Brothers – Isilingo

A ‘Soul Brothers‘ tune selected by www.Free-Mail.co.za: Soul Brothers – Isilingo …

Read moreSoul Brothers – Isilingo

About Alan

Previous Post:NYC – Civic Center: African Burial Ground National Monument
Next Post:Nice IMG_8675 photo

Reach Trust

Reach Trust is a holding trust for a number of companies within the Stratlec Group.

Copyright © 2026 · Free-Mail · All Rights Reserved · Powered by Straton Electrical

Back to top