Line wrapping text made easy with fold
Line wrapping text from the command line is easy with the fold utility, which of course is provided by the Free Software Foundation. By default, the fold command will wrap text at 80 characters, but you can of course specify the width manually. I prefer using the -s option, which will break only on spaces, making sure not to break in the middle of a word.
For example, the following command will concatenate a text file to standard output, adding line breaks only at spaces or at 72 characters, whichever comes first.
fold -s -w 72 textfile.txt
This can also be useful if you want to clearsign a message with Gnupg, but wish to line wrap it beforehand.
fold -s -w 72 textfile.txt | gpg --clearsign -u [email protected]
Add redirection if you wish to output the results to a file.
fold -s -w 72 textfile.txt > newfile.txt
As mentioned here, the fmt command also provides the same primary features of fold, but is much better. Not only does it wrap long lines, but it also fills out short lines as well. There are additional options that are worth looking into. Be sure to check out the man page!
man fmt
Add A Comment