home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 30.25 vi -r May not Write Recovered Buffer When You Exit Chapter 30
vi Tips and Tricks
Next: 30.27 vi Compound Searches
 

30.26 Shell Escapes: Running One UNIX Command While Using Another

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, called job control (12.1 ) , that works on many UNIX systems with most of their shells. I think that job control is a lot 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:

  1. Be sure you're in command mode (press the ESC key if you aren't sure).

  2. 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 before the grep command below.) Type :! followed by the UNIX command, then press RETURN. For example:

    ~
     
    :!grep tim 
    
    ~
    
    /phone
    
    

  3. 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:

:!pg somefile

Page through somefile on your screen.

:!ptroff %

Run this file through the ptroff formatter program. vi replaces % with the name of the file you're editing now.

:!mail

Read your mail. Be careful about this if you were already running the mail program, and you used the command ~v to edit a message with vi from inside the mail program. This shell escape starts a subshell (38.4 ) ; it will not take you back to the same mail session before you started editing!

:sh

Start a completely new shell. (If you are using a shell with job control, you'll almost always want to use job control to temporarily suspend vi instead (12.4 ) !)

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


Previous: 30.25 vi -r May not Write Recovered Buffer When You Exit UNIX Power Tools Next: 30.27 vi Compound Searches
30.25 vi -r May not Write Recovered Buffer When You Exit Book Index 30.27 vi Compound Searches

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System