204

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.

5 Answers 5

255

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

10
  • 3
    On a side note, to nicely format e-mails for text-only reply, I use: fold -s -w 80 email.txt | sed 's/^.*$/> &/' Feb 10, 2015 at 21:10
  • 3
    @MarcelloRomani, shouldn't you use a width of 78 since you're prepending two characters?
    – nanny
    Feb 26, 2015 at 14:59
  • 2
    Is there something like fold that lets you specify a string to wrap on?
    – will
    Feb 1, 2017 at 2:30
  • 5
    Note that fold breaks urls, while fmt does not. Mar 28, 2017 at 11:05
  • 2
    Works nicely in mingw, but it leaves trailing whitespace at the end of some lines. Is there a neat way of fixing this? Feb 9, 2020 at 16:32
75

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
  • 5
    I especially like fmt -t compared to fold
    – lkraav
    Dec 24, 2012 at 21:26
  • fmt works well with markdown-style paragraphs as well (though I would recommend manual wrapping on sub-sentence borders)
    – hoijui
    Nov 26, 2021 at 20:04
  • 1
    It also looks like fmt does not leave trailing spaces behind like fold does. Jan 25, 2022 at 17:57
45
$ cat shxp.txt

O, they have lived long on the alms-basket of words, I marvel thy
master hath not eaten thee for a word; for thou art not so long by the
head as honorificabilitudinitatibus: thou art easier swallowed than a
flap-dragon.

1​) Assured fixed line width with word breaking:

fold -w 20 <shxp.txt

O, they have lived l
ong on the alms-bask
et of words, I marve
l thy master hath no
t eaten thee for a w
ord; for thou art no
t so long by the hea
d as honorificabilit
udinitatibus: thou a
rt easier swallowed
than a flap-dragon.

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 20 <shxp.txt

O, they have lived
long on the
alms-basket of
words, I marvel thy
master hath not
eaten thee for a
word; for thou art
not so long by the
head as
honorificabilitudini
tatibus: thou art
easier swallowed
than a flap-dragon.

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 20 <shxp.txt

O, they have
lived long on the
alms-basket of
words, I marvel
thy master hath
not eaten thee
for a word; for
thou art not so
long by the head as
honorificabilitudinitatibus:
thou art easier
swallowed than
a flap-dragon.

Note that fmt also tries to balance ragged paragraph lines unlike fold -s.

2
  • 3
    Underrated answer. Available on most systems. Nice one.
    – Merc
    Oct 4, 2016 at 2:09
  • 2
    I really appreciate seeing a real text example with different options. I have been trying to write a Python version of wrap, but was unsatisfied with the long-word handling. Having the wrap and the fwt option for longer-than-specified words is very nice. Sep 4, 2020 at 18:31
14

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
1
  • 1
    Does not appear to be apart of Ubuntu 20.04.
    – Dave
    Apr 27, 2022 at 1:02
11

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

1
  • 4
    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, 2016 at 16:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .