Tell me more ×
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

3 Answers

up vote 7 down vote accepted

You are looking for

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

That's the way it's called on debian/ubuntu there are other systems, which need "-c" instead of "-w".

share|improve this answer

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

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

share|improve this answer

Your Answer

 
discard

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.