Sometimes when I do a filter-through-especially on some buggy, old
versions of vi-it completely scrambles and trashes my text.
Things can be so bad that the u (undo) command won't work.
Unless I've been careful to write my buffer (with
before the filter-through,
I can lose the whole file!
I've seen the same problem on several versions of vi and read
articles on Usenet from people who've been bitten by it.
If you've been burned, too, you might want to use thebelow. Map one of your keys - a numbered function key like F4, if you can - to do filter-throughs of your whole file. To start a filter-through, press the F4 key - this invokes the first map below. Next, type the UNIX command you want to run. To run the filter-through, press F4 again.
The maps are shown here:
enter the control characters
map #4 :se noaw^M:w^M:%d^M:r ! map! #4 ^V '%'^M:1d^[:se aw^[
The first one (
map #4) maps the F4 key during command mode to
set the noautowrite option (
:se noaw), write the buffer to
your file (
:w), delete all lines in the buffer (
and start a shell command line
After pressing F4 from command mode, your cursor should be on the bottom
line, ready for you to type the filter-through:
Type the UNIX command (like
fmt -75, and so on).
Then press F4 again.
Because you're in text-input mode, the second map above (
will be used this time.
The map starts with CTRL-v and a space; this trick puts a space between
the command line you type and the filename.
Next, the current filename
:r ! leaves a blank line above;
:1d deletes that line (it'll be the line 1 in the buffer).
:se aw command turns on the autowrite option
CAUTION: If you never set the autowrite option, you can leave out the
:se aw^M. But be sure - if autowrite is set when you use this map, all lines in your file will be deleted! (The empty buffer will be auto-written when the shell command runs.)
I guess it figures
tricky set of keymaps doesn't work perfectly on
every version of vi.
On my old SunOS 4.1 version of vi, for instance, it left me in text-input mode;
I had to hit ESC after pressing F4 the second time.
Still, this is a lot more reliable than the normal way to do
filter-throughs on buggy versions of vi.
It's been especially nice because I've always been able to undo the
filter-through with the u (undo) or
:e! commands. I
haven't used these keymaps in several years, but filter-throughs are
such an important vi feature to me that I'll always keep them handy.