Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 79 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 '15 at 21:10
@MarcelloRomani, shouldn't you use a width of 78 since you're prepending two characters? – nanny Feb 26 '15 at 14:59
Hmm... I guess so. Thanks for pointing that out :) – Marcello Romani Feb 27 '15 at 12:35

In addition to fold, take a look at fmt. fmt tries to choose line breaks intelligently to make text look good. It doesn't break long words, rather it wraps only by spaces. 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

1) assured fixed line width with word breaking:

fold -w 80 <text.txt

2) assured fixed line width with extraordinary word breaking (a word gets broken only if it is too large to fit in a line):

fold -sw 80 <text.txt

3) promising fixed line width without any word breaking (if word is too large to fit in a line, it is still left as it is, so finally some lines may be larger in size than you need):

fmt -w 80 <text.txt
share|improve this answer
fold -w 100 -s text.txt

worked for me as I need splitting every line till 100 characters

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.