The following expression is reasonably effective at wrapping long lines of text (for the purpose of dumping to my 128 characters wide terminal window and reading):
s~(.{104,124}) ~\1\n~g
This replaces the last space character in a “wrapping window” towards the end of the terminal window line with a newline character.
The only shortcomings that matter in practice are:
Long sequences of characters with no space inbetween occurring in the “wrapping window” will prevent wrapping. While certainly a shortcoming, in practice, this is not a real problem, at least not with typical word length encountered in English text.
Unnecessary wraps occur when the final word of the input line falls into the aforementioned “wrapping window” and the space is replaced by a newline in order to satisfy the expression. This can be observed fairly frequently. It is just a cosmetic nuisance, but still, it is a constant reminder of the imperfection of my pragmatic solution.
Is there an easy fix to prevent problem (2) from occurring, handling the special case of the “final word”? I'm only interested in GNU sed solutions; I know more can be done in PERL or other more heavy-weight tools.
fold -sw 115heavyweight?fold(from GNU coreutils) or, better yet, par. fold isn't much better than your sed s/// operation, but par is extremely flexible and very smart about how it wraps lines.sedtool. C'est de l'art pour l'art, not a practically important question. Thanks for suggestingfoldand especiallypar, which I was unaware of. I'm seeingfoldis contained in Git for Windows, which is what I'm using; forpar, I would have to install Cygwin or try building myself.sed.