7.2 Executing UNIX Commands
You can display or read in the results of any UNIX
command while you are editing in vi
.
An exclamation mark ( :! So if you are editing and you want to check the time or date without exiting vi , you can enter: :!date The time and date will appear on your screen; press [RETURN] to continue editing at the same place in your file. If you want to give several UNIX commands in a row without returning to vi editing in between, you can create a shell with the ex command: :sh When you want to exit the shell and return to vi , press [CTRL-D] .
You can combine :r !date will read in the system's date information into the text of your file.
By preceding the Suppose you are editing a file and want to read in four phone numbers from a file called phone , but in alphabetical order. phone reads: Willing, Sue 333-4444 Walsh, Linda 555-6666 Quercia, Valerie 777-8888 Dougherty, Nancy 999-0000 :r !sort phone reads in the contents of phone after they have been passed through the sort filter: Dougherty, Nancy 999-0000 Quercia, Valerie 777-8888 Walsh, Linda 555-6666 Willing, Sue 333-4444 Suppose you are editing a file and want to insert text from
another file in the directory, but you can't remember the new file's name.
You could
perform this task the long way: exit your
file, give the Or you could do the task in fewer steps:
7.2.1 Filtering Text Through a CommandYou can also 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 . 7.2.1.1 Filtering text with exThe first example demonstrates how to filter text with ex . Assume that the list of names in the preceding example, instead of being contained in a separate file called phone , is already contained in the current file on 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 filter and replace those lines with the output of sort . 7.2.1.2 Filtering text with viIn 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: !) will pass the next sentence through command . There are a few unusual features about how vi acts when you use this feature:
As another example, assume you have a portion of text in a file
that you want to change from lowercase to uppercase letters.
You could process that portion with the
To repeat the previous command, the syntax is: ! It is sometimes useful to send sections of a coded document to nroff to be replaced by formatted output. (Or when editing electronic mail, you might use the fmt program to "beautify" your text before sending the message.) 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. |
|