submt() { # # Format text to a desired line length, mindful of words & paragraphs. # # use: [ ll=N ] submt [ file | <file ] # filename as argument to edit the file itself, stdin for stdout # ll is line length, if not width of terminal # bare-bones default width, bring your own use case and sanity checks [ "$ll" ] || { ll=$(( tty=${COLUMNS:-$(tput cols)}, tty?tty-2:55 )) # fallback value from typography guidelines, or vague memory thereof } sed -nr${1+i} ' 1x 1!{ /^$/!H /^$/{ $!n x s/([^ -]|--)\n/\1 /g s/([^\n])\n/\1/g s/(.{0,'${ll}'})([ -]|$)/\1\2\n/g p } } ${ x s/([^ -]|--)\n/\1 /g s/([^\n])\n/\1/g s/(.{0,'${ll}'})([ -]|$)/\1\2\n/g s/\n$//p }' "$@" [ ! "$1" ] && printf '\n' }