[Chapter 30] 30.22 Filtering Text Through a UNIX Command
Author: Jerry Peek, Tim O'Reilly & Mike Loukides
Date: 1998-08-04T21:45:38Z

#

| Chapter 30 vi Tips and Tricks |

## 30.22 Filtering Text Through a UNIX Command

Warning! | When you're editing in vi, you can send a block of text as standard input to a UNIX command. The output from this command replaces the block of text in the buffer. You can filter text through a command from either ex or vi. The main difference between the two methods is that you indicate the block of text with line addresses in ex and with text objects (movement commands) in vi.

### 30.22.1 Filtering Text with ex

The first example demonstrates how to filter text with ex. Assume that you're editing a file with a list of names and you want to alphabetize (sort) lines 96 through 99. You simply type the addresses of the lines you want to filter, followed by an exclamation mark and the UNIX command to be executed. For example, the command:

>     :96,99!sort

will pass lines 96 through 99 through the sort (36.1) filter and replace those lines with the output of sort.

The ex `%` operator is the easiest way to filter all the lines in your buffer. If you're editing a C program, for instance, you could feed it all through the formatting program named indent by typing:

>     :%!indent

### 30.22.2 Filtering Text with vi

In vi, text is filtered through a UNIX command by typing an exclamation mark (`!`) followed by any of vi's movement keystrokes that indicate a block of text, and then by the UNIX command line to be executed. For example:

>     !)command

will pass the next sentence through command.

There are a couple of unusual features about how vi acts when you use this structure.

- First, the exclamation mark doesn't appear on your screen right away. When you type the keystroke(s) for the text object you want to filter, the exclamation mark appears at the bottom of the screen, but the character you type to reference the object does not.

- Second, text blocks must be more than one line, so you can use only the keystrokes that would move more than one line ( `G`, `{ }`, `( )`, `[[ ]]`, `+`, `-` ). To repeat the effect, a number may precede either the exclamation mark or the text object. (For example, both `!10+` and `10!+` would indicate the next ten lines.) Objects such as `w` do not work unless enough of them are specified so as to exceed a single line. You can also use a slash (`/`) followed by a pattern and a carriage return to specify the object. This takes the text up to the pattern as input to the command.

- Third, there is a special text object that can be used only with this command syntax; you can specify the current line by entering a second exclamation mark:

  >     !!command

  Remember that either the entire sequence or the text object can be preceded by a number to repeat the effect. For instance, to change lines 96 through 99 as in the previous example, you could position the cursor on line 96 and enter either:

  >     4!!sort

  or:

  >     !4!sort

As another example, assume you have a portion of text in a message that you're going to send to the Usenet (1.33) "net news." On Usenet, text that might be offensive or would give away the answer to a question is "rotated." The letter a is changed to n, b is changed to o, and so on. There are programs for rotating text, but it's also easy to rotate text with the tr (35.11) command. In this example, the second sentence is the block of text that will be filtered to the command.

>      One sentence before.
>      With a screen editor you can scroll the page
>      move the cursor, delete lines, insert characters,
>      and more, while seeing the results of your edits
>      as you make them.
>      One sentence after.
Keystrokes | Results
> `!)` |
>      One sentence after.
>      ~
>      ~
>      ~
>      !_
| An exclamation mark appears on the last line to prompt you for the UNIX command.
> `tr '[a-z]' '[A-Z]'` |
>      One sentence before.
>      WITH A SCREEN EDITOR YOU CAN SCROLL THE PAGE
>      MOVE THE CURSOR, DELETE LINES, INSERT CHARACTERS,
>      AND MORE, WHILE SEEING THE RESULTS OF YOUR EDITS
>      AS YOU MAKE THEM.
>      One sentence after.
| Enter the UNIX command and press RETURN. The input is replaced by the output.

To repeat the previous command, the syntax is:

>     ! object !

It is sometimes useful to send sections of a coded document to nroff to be replaced by formatted output. Remember that the "original" input is replaced by the output. Fortunately, if there is a mistake, such as an error message being sent instead of the expected output, you can undo the command and restore the lines.

- TOR from O'Reilly & Associates' Learning the vi Editor, Chapter 7

30.21 Per File Setups in Separate Files |  | 30.23 Safer vi Filter-Throughs