Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I know that I can use something like cat test.txt | pr -w 80 to wrap lines to 80 characters wide, but that puts a lot of space on the top and bottom of the printed lines and it does not work right on some systems

What's the best way to force a text file with long lines to be wrapped at a certain width?

Bonus points if you can keep it from breaking words.

share|improve this question

4 Answers 4

up vote 38 down vote accepted

You are looking for

fold -w 80 -s text.txt
  • -w tells the width of the text, where 80 is standard.
  • -s tells to break at spaces, and not in words.

This is the standard way, but there are other systems, which need "-c" instead of "-w".

share|improve this answer
Works on OS X, too, but filename needs to be after args. Thanks! –  rdrey Sep 2 '14 at 22:13
On a side note, to nicely format e-mails for text-only reply, I use: fold -s -w 80 email.txt | sed 's/^.*$/> &/' –  Marcello Romani Feb 10 at 21:10
@MarcelloRomani, shouldn't you use a width of 78 since you're prepending two characters? –  nanny Feb 26 at 14:59
Hmm... I guess so. Thanks for pointing that out :) –  Marcello Romani 2 days ago

In addition to fold, take a look at fmt. fmt tries to choose line breaks intelligently to make text look good. It will also join adjacent lines, which is good for prose but bad for log files or other formatted text.

share|improve this answer
I especially like fmt -t compared to fold –  lkraav Dec 24 '12 at 21:26

Another (less known) tool that does what you want is wrap from GNU Talkfilters:

wrap -w 80 < textfile

Also (off topic):

but that puts a lot of space on the top and bottom of the printed lines

add -t when invoking pr to omit headers/trailers:

   -t, --omit-header
          omit page headers and trailers
share|improve this answer

And for more formatting options, look at par -- http://www.nicemice.net/par/

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.