ufmt

ufmt [1] is a command-line plain text formatter. It reformats text so that it's line widths are as close to the given goal width as possible.

ufmt is close in function to GNU fmt [2] and BSD fmt [3]; it's advantages are:

In short, ufmt can turn a suboptimally formatted chunk of text like this:

/* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Aliquam placerat faucibus luctus.
 * Donec dolor lorem,
 * posuere sit amet erat vel, lobortis feugiat libero.
 *
 * Phasellus dui lorem, auctor at luctus scelerisque,
 * aliquam non nulla:
 *
 * - Sed laoreet enim vel ipsum ultrices rutrum.
 *   Proin sit amet eros a lectus bibendum efficitur
 *   non sit amet libero.
 *
 * - Proin vel commodo odio. Proin eu volutpat augue.
 *   Nullam a blandit nunc, sed dictum odio.
 *   Ut erat libero, ullamcorper in purus at, finibus rutrum sapien.
 */

Into this:

/* Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Aliquam placerat faucibus luctus. Donec dolor lorem,
 * posuere sit amet erat vel, lobortis feugiat libero.
 *
 * Phasellus dui lorem, auctor at luctus scelerisque, aliquam
 * non nulla:
 *
 * - Sed laoreet enim vel ipsum ultrices rutrum. Proin sit
 *   amet eros a lectus bibendum efficitur non sit amet
 *   libero.
 *
 * - Proin vel commodo odio. Proin eu volutpat augue. Nullam
 *   a blandit nunc, sed dictum odio. Ut erat libero,
 *   ullamcorper in purus at, finibus rutrum sapien.
 */

Be careful though: ufmt only recognizes paragraphs if they are separated by "blank" lines (i.e. lines with no alphanumerical characters in them), which is not always how paragraphs are formatted in practice. Also, the preservation of line indents may lead to undesirable results if the leading non-alphabumerical sequence was not meant to be an indent.

Just as with other command-line text formatters, you are advised to only use ufmt interactively (i.e. by formatting text fragments from your editor), and always make sure the output is what you want it to be.

How to build it

ufmt is written in OCaml. To build it, you need to install uucp, uuseg and uutf libraries using OPAM [4]. You will also need ocamlbuild (either from OPAM or from your package manager).

If you've got all of that ready, clone ufmt sources like this:

hg clone http://hg.tx97.net/ufmt

... and build it by just running make.

How to use it

Short usage:

Usage:
    ufmt [-h] [-g width] [-w width] [filename ...]
Options:
    -h          show this helpful message
    -g width    use this goal line width (default: 62)
    -w width    do not exceed this line width (default: 72)

For more detailed usage see the man page at [5].

References

  1. http://hg.tx97.net/ufmt/
  2. https://www.gnu.org/software/coreutils/manual/html_node/fmt-invocation.html
  3. https://www.freebsd.org/cgi/man.cgi?query=fmt
  4. http://opam.ocaml.org/
  5. http://manweb.tx97.net/http://hg.tx97.net/ufmt/raw-file/tip/ufmt.1