138

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.

173

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".

  • Works on OS X, too, but filename needs to be after args. Thanks! – rdrey Sep 2 '14 at 22:13
  • 2
    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
  • 2
    @MarcelloRomani, shouldn't you use a width of 78 since you're prepending two characters? – nanny Feb 26 '15 at 14:59
  • 1
    Hmm... I guess so. Thanks for pointing that out :) – Marcello Romani Feb 27 '15 at 12:35
  • 2
    Note that fold breaks urls, while fmt does not. – Skippy le Grand Gourou Mar 28 '17 at 11:05
51

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.

  • 3
    I especially like fmt -t compared to fold – lkraav Dec 24 '12 at 21:26
14

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
  • Underrated answer. Available on most systems. Nice one. – Merc Oct 4 '16 at 2:09
11

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
5

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

  • 2
    Currently the web site is down, there is the Internet Archive and Google's cache but still this shows why it's important to post more than just links, you could have at least posted the examples from the official documentation. – phk Dec 27 '16 at 16:31
-1
fold -w 100 -s text.txt

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

  • What does your answer add that the others did not? – Sheljohn Apr 7 '18 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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