Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Wrap Text to X Characters on the Command Line with fold or fmt

blog/cards/wrap-text-to-x-characters-on-the-command-line-with-fold-or-fmt.jpg

They're core GNU tools that you likely already have available. It's handy to make output more readable on high resolution displays.

Quick Jump: Demo Video

Both fold and fmt can help you take a really long line and wrap it to let’s say 80 characters or whatever width you prefer.

For example, try pasting these commands into your terminal:

# No formatting.
$ echo "Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen."

Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen.

# Wrapping text with fold.
$ echo "Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen." | fold -s

Hello world, this is a pretty long sentence that is surely going to be beyond
80 characters long but look how hard it is to read in a full size terminal on a
large screen.

# Wrapping text with fmt.
$ echo "Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen." | fmt -80

Hello world, this is a pretty long sentence that is surely going to be beyond
80 characters long but look how hard it is to read in a full size terminal
on a large screen.

fold defaults to 80 characters where as modern versions of fmt defaults to 75. Both tools let you pass in the number of characters you want per line using -X or -w X where X is the character count.

If you check the fmt command’s output above, it’s still slightly less than 75 characters on the second line. That’s because modern versions of this tool have a secondary -g argument which is the goal width percent that defaults to 93%, which would be 70 characters.

The fold command above is also using -s to ensure words are broken up by a space. Without this option you would end up with words being split across multiple lines which you can see below:

$ echo "Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen." | fold

Hello world, this is a pretty long sentence that is surely going to be beyond 80
 characters long but look how hard it is to read in a full size terminal on a la
rge screen.

fold is POSIX compliant. It’ll work the same across its FreeBSD and GNU versions, fmt on the other hand is quite a bit different across both versions so if maximum compatibility is required then I’d suggest fold.

fmt has a few neat features:

One of which is if your text is less than the characters you want to wrap, it will join those lines. That may or may not be what you want but it’s there if you need it:

$ echo -n "Hello\nWorld\n" | fold
Hello
World

$ echo -n "Hello\nWorld\n" | fmt
Hello World

If you want to skip this behavior with fmt you can run fmt -s which splits only long lines.

You can also squish multiple spaces into 1, for example:

# Not uniform.
$ echo "Hello    world. This is a sentence." | fmt
Hello    world. This is a sentence.

# Uniform.
$ echo "Hello    world. This is a sentence." | fmt -u
Hello world. This is a sentence.

Depending on which version of fmt you have, it may also put 2 spaces before the start of sentences. I am using (GNU coreutils) 8.32 which didn’t, you can check with fmt --version.

Which one should you use?

It really depends.

I mostly use either or when I have a large chunk of output that I want to read more comfortably. Usually if it’s a 1-off command I’ll go with fmt since formatting text as a concept aligns with my mental mode of what this does vs fold.

On the other hand fold gives you 5 more characters per line by default and would be nicer to type than fmt -80 if you really wanted those extra characters but since fmt wraps words out of the box without needing -s I prefer that in the end. For 1-off commands it doesn’t really bother me that it’s 75 instead of 80 characters so I still end up using fmt.

If you really cared about always using a custom width you can create an alias too.

If you want consistent behavior in a script across multiple operating systems then I’d use fold over fmt unless I knew for sure I was targeting the GNU version of fmt on a modern’ish system and I needed fmt’s extra features.

If you need to squish text then fmt can do that out of the box.

A friendly reminder about the Unix philosophy:

If you want to wrap long text to multiple lines and have each line prefixed with the line count you can do that by piping it into cat -n:

$ echo "Hello world, this is a pretty long sentence that is surely going to be beyond 80 characters long but look how hard it is to read in a full size terminal on a large screen." | fmt | cat -n
     1  Hello world, this is a pretty long sentence that is surely going to be
     2  beyond 80 characters long but look how hard it is to read in a full size
     3  terminal on a large screen.

The video below goes over running all of the commands and checking out the man page for both tools.

Demo Video

Timestamps

  • 0:51 – Using fold
  • 2:29 – Using fmt
  • 3:19 – fmt works a bit different depending on your version
  • 4:47 – Squishing spaces with fmt
  • 6:08 – Optionally making shorter lines longer with fmt
  • 7:22 – Which one should you use?
  • 7:31 – Combining either one with cat to output line counts

When was the last time you used these tools? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments