alexkarle.com

Source for alexkarle.com
git clone git://git.alexkarle.com/alexkarle.com.git
Log | Files | Refs | README | LICENSE

hard-wrapping-in-vi-and-vim.txt (1426B) [raw]


      1 Hard-Wrapping Lines in vi(1) and vim(1)
      2 ---------------------------------------
      3 Wed Dec  1 00:00:50 EST 2021
      4 
      5 tags: editors
      6 
      7 There's several types of text that I really like to hard-wrap.
      8 Phlog posts, personal notes, git messages, and emails to name
      9 a few.
     10 
     11 This can be done in vim(1) by setting `textwidth` to 60 and
     12 then using the `gq<motion>` command to format (example: `gqip`
     13 formats *i*nside *p*aragraph).
     14 
     15 However, I really like using OpenBSD's vi(1) as my $EDITOR due
     16 to its blazingly fast startup time (and the whole point of the
     17 $EDITOR is that it is executed by other programs!).
     18 
     19 To achieve the same automated formatting, I make use of a
     20 lesser known feature:
     21 
     22 	! <motion> command ...
     23 
     24 Will replace the lines in <motion> with the output of the shell
     25 command (which receives the lines as standard input).
     26 
     27 Better yet, this repeats with the dot command. So, as a final
     28 action before publishing, I go to the top of the first paragraph
     29 and hit `!}fmt 60`, then go down the post with `}.}.}.` until
     30 all paragraphs are wrapped.
     31 
     32 How cool is that? It even works in vim(1) (one of the perks of
     33 learning plain-vi(1) is discovering deeper vim(1) features!)
     34 
     35 ---
     36 
     37 Note that you can also use:
     38 
     39 	range ! command
     40 
     41 Such as `%!fmt 60` to filter the whole document, but I find
     42 there's usually bits and pieces fmt(1) doesn't handle well
     43 (such as title underlines or ascii art), so I prefer to
     44 quickly format each paragraph by hand.