Formatting file in command line for printing

.lpcupslprprintingtext formatting

Whenever I print a text file with the lpr or lp commands, the words are cut off at the end of one line and continue onto the other; for example, 'understand' would be split into 'unde' at the end of line one and 'rstand' at the beginning of other. Is there a way to justify the text of a file somehow for printing ? I've tried lpr -p and -o media=a4, and fit-to-page options, but the words are still cut off.

Solutions that worked for me:

  1. garethTheRed's fold: fold -s textfile.txt | lpr
  2. fmt command found here and here: fmt -u -w 80 textfile.txt | lpr ; note , that width 80 could be changed to whatever you like, but for me this seems to work well enough

Best Answer

Use fold. Extracts from the man page:

Wrap  input  lines in each FILE (standard input by default), writing to
standard output.

-b, --bytes
   count bytes rather than columns

-c, --characters
   count characters rather than columns

-s, --spaces
    break at spaces

-w, --width=WIDTH
    use WIDTH columns instead of 80

Use fold (maybe using the -s option so that it doesn't break your lines mid-word) to set your document to about 80 characters wide and print:

fold -s myfile.txt | lpr

Or, to save the formatted version:

fold -s myfile.txt > output.txt
Related Question