Some Unix commands (usually interactive commands like vi) let you run another Unix command temporarily. To do that, you type a special command character -- usually an exclamation point (!) -- then type the Unix command line you want to run. In this article, I'll show examples for the vi editor. To see if this works on another utility, check its documentation or just try typing !Unixcommand when the utility is waiting for you to type a command.
You can run any Unix command without quitting vi. That's handy, for example, if you want to read your mail or look at some other file . . . , then go back to the file you were editing without losing your place. It's called a "shell escape." (By the way, there's a another way to do this, job control (Section 23.3), that works on most Unix systems. Job control is often more convenient and flexible than shell escapes.)
Let's say you're editing the file named foo and you need to run grep to get someone's phone number from your phone file. The steps are as follows:
Be sure you're in command mode (press the ESC key if you aren't sure).
If you want to run a command that needs the file you're editing, remember to write out your vi buffer with the :w command. (So you probably wouldn't need to write anything before the following grep command.) Type :! followed by the Unix command, then press RETURN. For example:
:!grep tim ~/phone
The grep program will run. When it finishes, vi will say:
[Hit return to continue]
After you press RETURN, you'll be right back where you were.
Other examples:
Basically, anything you can do at a shell prompt, you can do with a shell escape. You'll be in a subshell though, not your original login shell. So commands like cd won't affect the program where you started the subshell or any other shell. On the bright side, changing directories or resetting anything in your environment won't affect vi or the shell where you started vi. Terminating the program you're running in the subshell will bring you right back where you were.
-- JP
Copyright © 2003 O'Reilly & Associates. All rights reserved.