home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.22 Filtering Text Through a UNIX Command Chapter 30
vi Tips and Tricks
Next: 30.24 vi/ex File Recovery vs. Networked Filesystems
 

30.23 Safer vi Filter-Throughs

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 :w ) 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 the keymaps (31.2 ) below. 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 ^M ^V , and ^[ by typing CTRL-v first (31.6 ) .


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 (:%d ), and start a shell command line (:r ! (33.4 ) ). After pressing F4 from command mode, your cursor should be on the bottom line, ready for you to type the filter-through:

:r !

Type the UNIX command (like expand , fmt -75 , and so on). Then press F4 again. Because you're in text-input mode, the second map above (map! #4 ) 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 (% (30.4 ) ) -in single quotes, to protect special characters from the shell - is output, followed by a RETURN to execute the command. Reading a UNIX command with :r ! leaves a blank line above; the :1d deletes that line (it'll be the line 1 in the buffer). Finally, the :se aw command turns on the autowrite option again.

CAUTION: If you never set the autowrite option, you can leave out the :se noaw^M and :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 :-( -this 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.

- JP


Previous: 30.22 Filtering Text Through a UNIX Command UNIX Power Tools Next: 30.24 vi/ex File Recovery vs. Networked Filesystems
30.22 Filtering Text Through a UNIX Command Book Index 30.24 vi/ex File Recovery vs. Networked Filesystems

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System