Hard-Wrapping Lines in vi(1) and vim(1)
---------------------------------------
date: 2021-12-01
tags: editors

There's several types of text that I really like to hard-wrap.
Phlog posts, personal notes, git messages, and emails to name
a few.

This can be done in vim(1) by setting `textwidth` to 60 and
then using the `gq<motion>` command to format (example: `gqip`
formats *i*nside *p*aragraph).

However, I really like using OpenBSD's vi(1) as my $EDITOR due
to its blazingly fast startup time (and the whole point of the
$EDITOR is that it is executed by other programs!).

To achieve the same automated formatting, I make use of a
lesser known feature:

       ! <motion> command ...

Will replace the lines in <motion> with the output of the shell
command (which receives the lines as standard input).

Better yet, this repeats with the dot command. So, as a final
action before publishing, I go to the top of the first paragraph
and hit `!}fmt 60`, then go down the post with `}.}.}.` until
all paragraphs are wrapped.

How cool is that? It even works in vim(1) (one of the perks of
learning plain-vi(1) is discovering deeper vim(1) features!)

---

Note that you can also use:

       range ! command

Such as `%!fmt 60` to filter the whole document, but I find
there's usually bits and pieces fmt(1) doesn't handle well
(such as title underlines or ascii art), so I prefer to
quickly format each paragraph by hand.