Creating Text Processing Tools with Vim

2022-12-03

To create a Vim script for text processing, such as automatically wrapping text at the 70th column, follow these steps:

:set tw=70
gggqG
:wq

Save it as ~/.vim/scripts/wrap, then you can process files with:

vim -s ~/.vim/scripts/wrap input.txt

If you want to input from stdin and output to stdout, which is suitable for piping to other programs, wrap it in a bash script:

#!/bin/bash

BUF=/tmp/$(head -c 15 /dev/urandom | base32)
cat > $BUF
/usr/bin/vim -s ~/.vim/scripts/wrap $BUF 1>/dev/null 2>/dev/null
cat $BUF
rm $BUF

With this, a small tool is done.



Email: i (at) mistivia (dot) com