Filtering text within the file through another program - the ! operator.



Last revision August 2, 2004

Table of Contents:
  1. Editor choices on Unix
  2. Characteristics, advantages, and disadvantages of vi
  3. Basic text editing operations in vi
  4. Regular expressions
  5. File searching with grep
  6. More about regular expressions
  7. Intermediate text editing with vi
  8. Vi Quick Reference

The filter command uses the exclamation point (!) as an operator, just like the d or c command. It can work with any object or repeat count to specify how much text to affect. Its function is to take the selected text, pass it as standard input to some other Unix program that you specify on the command/status line, and then replace that text in the file with the output of the program.

For example, if you type the vi command

5!!

This means that you want to pass the five lines starting at the current cursor position to another program and replace them with the output of that program.

Whenever you type a ! filter command, the cursor jumps to the command/status line, prints a ! character to remind you of what you are doing, and then lets you type in any Unix command, or sequence of commands in a pipeline, that will take data from standard input and write results to standard output. Just about any Unix program will use standard input and standard output. You press the RETURN key when you have finished typing my command to indicate that it should be executed.

vi then starts up the specified program or pipeline, feeds the specified text from the file (such as the five lines from 5!!) as standard input to that command, and then replaces that text in the file with whatever is written to standard output by that command.

The ! filter command allows you to extend the editor to include all kinds of functions that are not built-in to the editor, but are available in other Unix commands or utilities. For example, there is a sort program in Unix to sort the lines in a file into alphabetical order. It operates as a filter, taking standard input if no filenames are specified, and writing the sorted lines to standard output. You can use it within the vi editor to sort lines without leaving the editor. For example, you can sort the 10 lines beginning at the cursor by giving the command:

10!!

The cursor will jump down to the command line, where you enter the sort command followed by a RETURN key:
sort <CR> You can also use a more complicated sort command with options that control sorting order, or pipe the output to another command that does further processing, all in that single command line.

Remember, the ! filter command is not the same as the :! shell escape command. ! is a standard vi operator (same syntax as d) that allows you to filter part of the text within the file through another Unix program. :! is an ex-style command that lets you run another Unix program, but without affecting the file or getting input from the file in any way (just a convenience to run some other program while temporarily suspending your vi editing session).

Comments or Questions?