ufmt [1] is a command-line plain text formatter. It reformats text so that its 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]; its advantages are:
Support for indented paragraphs: any run of non-alphanumeric characters at the start of the line is considered an indent; if a paragraph has a consistent ident of a given width, this indent is preserved in the output (the last indent sequence is repeated, if needed). This allows ufmt to correctly format e.g. C-style multiline comments or Markdown-style bullet item blocks.
Unicode support: input is assumed to be UTF-8, and line widths are calculated taking into account combining and multi-column characters.
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.
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.
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].