fmt: And the difference is. …

Sam asked a good question after the fold post from yesterday. What’s the difference between fold and fmt?

Here’s fold on the top and fmt on the bottom.

2013-11-06-lv-r1fz6-fmt

First, fmt doesn’t want to break words. fold will, given the opportunity, and actually has to be told to wrap at spaces.

fmt will also correct some spacing issues. Tacking on the -u option pulls extra spaces and fills two spaces after a period. (My secondary school typing teacher would be proud.)

Plus, fmt also has some more sophisticated “margin” handling. You can tell fmt to push to within X percent of the wrap limit — a “goal” — and then break a line. I’ve seen word processors with that kind of feature, usually for hyphenation.

And judging by the man page, fmt tries to avoid breaking lines at the first word or before the last word, meaning it has some rudimentary widow and orphan control.

fmt can also set special indentations, split but not refill lines and screen lines before applying formatting.

So I guess the answer is … fmt seems a bit more attuned to formatting text in paragraphs, and making it presentable. Like designing a help page or cleaning up wordy output.

fold on the other hand, appears more attuned to straight lines of text … perhaps annotated code or something like that.

I am a newcomer to both though, so if there’s another use for either, please let me know. 😉

P.S., does it surprise you that fmt is in coreutils too? It shouldn’t.

8 thoughts on “fmt: And the difference is. …

  1. MrFrood

    Probably worth mentioning that iirc `fold’ is more consistent in its usage across the various BSD’s/Linuces/Unices than `fmt’. Also with reference to this sort of text processing `par’ should be on your list for the P section when you reach it.

  2. Sam Rowe

    I use Mutt as my primary email, so I have this in my ~/.vimrc to format paragraphs to nice readable widths:

    map ^P !} fmt -w 72

    1. K.Mandla Post author

      Interesting. I have

      set lbr

      to avoid breaking words in vim, but I hadn’t thought to map the wrap action to a key. Is that similar to CTRL-J in pico or nano?

  3. Pingback: par: After pandoc, par for the course | Inconsolation

  4. Pingback: minime: The 9.8K text editor | Inconsolation

  5. Pingback: Tricks of the trade | Motho ke motho ka botho

Comments are closed.