The fmt command is a text utility included in the GNU Core Utilities. It was originally created to format email messages at the command line. However, it can be very useful for reading any text files in the terminal. Sure, modern terminals will wrap text to fit in the window. But they don't wrap at a word, it could split a word right down the middle. This makes it hard to read and even harder to keep your place.
While reading a short unformatted text file is possible, the longer the file gets the more difficult it becomes. This is where the fmt command saves the day.
Table of Contents
How to Format a Text File Using fmt
The default behavior of the fmt command is to break longs lines (and combine short lines... more on that later) to make the text 75 columns long. To do this basic formatting just pass the file name as a argument like so:
[mcherisi@putor ~]$ fmt time.txt
Ticking away the moments that make up a dull day, Fritter and waste the
hours in an offhand way. Kicking around on a piece of ground in your
home town, Waiting for someone or something to show you the way.
In the above example the fmt command formatted the text so the lines all fit within the default 75 columns. If we use the cat command, we can see that the file is one long unformatted line.
[mcherisi@putor ~]$ cat time.txt
Ticking away the moments that make up a dull day, Fritter and waste the hours in an offhand way. Kicking around on a piece of ground in your home town, Waiting for someone or something to show you the way.
NOTE: The above example is highly dependent on your resolution and window size.
Here is where the "default" behavior gets interesting. If you have a file with a bunch of lines that are shorter than 75 columns, then it will do the opposite. It will combine the short lines into longer lines up to the 75 column default. I know that seems complicated, let's take a look at an example.
Here is the first verse of the song Money by Pink Floyd.
[mcherisi@putor ~]$ cat money.txt
Money
Get away
You get a good job with good pay and you're okay
Money
It's a gas
Grab that cash with both hands and make a stash
New car, caviar, four star daydream
Think I'll buy me a football team
This file has 8 lines, the longest of which is 48 columns. In this case the fmt command will combine the text to make 3 lines that wrap at the 75th column.
[mcherisi@putor ~]$ fmt money.txt
Money Get away You get a good job with good pay and you're okay Money
It's a gas Grab that cash with both hands and make a stash New car,
caviar, four star daydream Think I'll buy me a football team
Setting Your Preferred Width
You can easily override the default width of 75 by passing the -w
or --width
options. This comes in handy if you are using a small terminal window or an old console with low resolution. In this example we will set the width to just 20 columns (an extreme example).
[mcherisi@putor ~]$ fmt -w 20 time.txt
Ticking away
the moments that
make up a dull
day, Fritter and
waste the hours
in an offhand way.
Kicking around on
a piece of ground
in your home town,
Waiting for someone
or something to
show you the way.
This will also have the opposite effect if the lines are shorter than 20.
[mcherisi@putor ~]$ cat fruits.txt
apples
oranges
bananas
[mcherisi@putor ~]$ fmt -w 20 fruits.txt
apples oranges
bananas
Above it combines the lines until it hit the 20 column width designation, then it wraps to the next line.
Create Uniform Spacing on Text Files
If you have a file with a lot of extra spacing or other formatting abnormalities, you can use the -u
or --uniform-spacing
options. This will format the file to have a single space after each word and two spaces after sentences.
[mcherisi@putor ~]$ cat test.txt
This is a file
with weird spacing.
This sentence has a tab
and several spaces.
[mcherisi@putor ~]$ fmt -u test.txt
This is a file with weird spacing. This sentence has a tab and several
spaces.
First Line Indentation
The fmt commands -t
or --tagged-paragraph
option will indent the first line of each paragraph. Or as they say in the man pages "indentation of first line different from the second".
Split Only - Do Not Refill
Using the -s
or --split-only
option will allow you to split long lines, without joining short lines to form longer ones.
[mcherisi@putor ~]$ fmt -s harrison-bergeron.txt
THE YEAR WAS 2081, and everybody was finally equal. They weren't only
equal
before God and the law. They were equal every which way. Nobody was
smarter
than anybody else. Nobody was better looking than anybody else. Nobody was
stronger or quicker than anybody else. All this equality was due to the
211th, 212th, and 213 th Amendments to the Constitution, and to the
unceasing
vigilance of agents of the United States Handicapper General.
In the example above the fmt command wraps the lines at the 75th column as usual. However, it did not combine the next lines to form a 75 column long line. Hence, the "split only"...
Conclusion
The fmt command is just another one of those perfectly quirky utilities that really can come in handy. A lot of this formatting can be done manually, but would be extremely tedious. If you want to master the command line, you will need to master the basic text utilities that come packaged with almost every system in the GNU Core Utilities.
Resources and Links
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (51)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)