Sending text through a filter
You can send some or all of the contents of the current file
through a program that acts as a filter, transforming the
contents of the current file. For example, to use the tr
command to translate the current paragraph into uppercase:
-
Enter command mode by pressing <Esc>, if necessary.
-
Go to the beginning of the first line of the paragraph.
-
Type the following characters:
!} tr '[a-z]' '[A-Z]'
The command ! tells
vi to filter all the text from the current cursor
position to the position indicated by the subsequent movement
command (in this case ``}'', the command to move to the
end of the paragraph), through the program tr with the arguments
'[a-z]' '[A-Z]'.
tr translates its input, defined by the
first wildcard expression, into an output file defined by the second
expression. The output from tr is then substituted for
the input.
In general, you can use any filter in this way:
! movement filter <Enter>
An exclamation point typed in command mode introduces a filter command.
movement is a vi command to move
the cursor to the end of the block of text you
want to feed through filter.
The cursor jumps to the bottom line of the screen when you
enter the movement command, and a ``!'' prompt appears;
vi waits for you to enter the filter command and
press <Enter>. Note that the cursor movement must refer to a block
of text larger than one word; the commands w and
e are not acceptable cursor movements.
Another common usage is to spell-check a sentence using
spell, and replace it with a list of all the unidentified words
it contains:
-
Enter command mode by pressing <Esc>, if necessary.
-
Move to the start of the sentence by pressing 0.
-
Enter the command:
!)spell <Enter>
The ) command moves the cursor to the end of the current
sentence. The !) command
selects the text from the current cursor position to the end of the
sentence and sends it to the standard input of
spell. The output from spell
(all the words in the sentence that it cannot find in its
dictionary) replaces the sentence and those below it with each
mispelled word that is found. Retrieve the original sentence
using the u (for Undo) command immediately.
For example:
When looking for occurences of spelling mistakes
Applying the above procedure replaces this sentence with the following:
occurences
The escape filter can be used with any command that reads from
the standard input. For
example, use wc to find the number of words in a file;
go to the top of the
document and issue the command !Gwc -w. The document is
replaced by a word count. Press u to get
your document back again.
Next topic:
Defining abbreviations
Previous topic:
Running other programs from inside vi
© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 03 June 2005