I would like to output a series of space-delimited words in a tabular format, filling line by line so that nothing exceeds the terminal's width but the available space is used optimally, like this:+-----------------------------------------------+|polite      babies      embarrass   rightful   | |aspiring    scandalous  mut         disgusted  ||bell        deeply      writer      jumbled    ||respired    craggy                             |(the box illustrates the terminal's width - it is not part of the output)The commands that spring to mind are fold and column in a pipeline like this:$ fold words -s -w $COLUMNS | column -tThis almost works but the output ends up wider than $COLUMNS (the terminal width) because it is first folded within that width and then the whitespace is stretched to line them up.What I need is the effect of both in one. Are there any command-line tools (or shell built-ins) that can do this?
How can a space-delimited list of words be folded into tabular columns that fit in the terminal's width
command line;text formatting
To produce equally spaced columns, you could use BSD rs (also ported to Debian and derivatives (at least) and available as a package there):tr -s '[:space:]' '[ *]' | rs -w$COLUMNSExample (vertical line not part of the output):$ lorem -w 30 | tr -s '[:space:]' '[ *]' | rs -w60earum          aspernatur     ipsa           sed            quod           sit            esse           quisquam       animi          reprehenderit  porro          et             delectus       neque          esse           quia           pariatur       amet           iste           voluptatem     provident      praesentium    et             sint           quo            animi          doloribus      veritatis      iusto          alias                                        You can add the -z option to reduce the space between the columns, but that does not optimise the number of columns accordingly. For instance, on the above, it gives (with rs -zw60):earum      aspernatur     ipsa       sed                    quod       sit            esse       quisquam               animi      reprehenderit  porro      et                     delectus   neque          esse       quia                   pariatur   amet           iste       voluptatem             provident  praesentium    et         sint                   quo        animi          doloribus  veritatis              iusto      alias                                            Instead of:earum      aspernatur   ipsa       sed    quod              sit        esse         quisquam   animi  reprehenderit     porro      et           delectus   neque  esse              quia       pariatur     amet       iste   voluptatem        provident  praesentium  et         sint   quo               animi      doloribus    veritatis  iusto  alias             It also doesn't work with multi-byte characters or 0-width or double-width characters.By default, it leaves at least 2 spaces between the columns. You can change it to 1 with -g 1.