Working with Basic Unix Applications

You learned earlier that several Unix applications are included with Mac OS X. Although you aren't likely to use these instead of your Mac OS X applications for your everyday work, sometimes these applications can be quite useful. For example, you might want to use the vi text editor to create Shell scripts. A couple of examples will show you how such applications work.

Editing Text with vi

The Unix application vi is a basic text editor. You can use it to create and edit text files, but it is most useful for creating Shell scripts. You are unlikely to use it to create text documents, but you can use it to create plain-text documents if you would like.

The vi program has two modes: Edit and Command. In Edit mode, you can enter and edit text. In Command mode, you issue commands to the program. Do the following:

  1. Type man vi. Read the manual pages to get an idea of how vi works.

  2. Open a new Terminal window (graphics/mac.gif-N) and type vi and the name of the text file you want to create, such as vi newtestfile.txt. The program opens, the file is created in the current directory, and you see a screen containing tilde symbols in the editing area. At the bottom of the screen, you will see the vi command line.

  3. Type i to enter Insert mode.

  4. Type your text.

  5. Press Esc to move into Command mode. While you are in Command mode, you will hear an alert sound if you try to type anything that isn't a recognized vi command; you will also see a prompt at the bottom of the vi window telling you that the text you typed isn't a recognized command.


    Determining which mode you are in can be confusing. When you enter Command mode, the cursor appears to jump back a couple of spaces and the bottom line of the window is empty. You can then type a command. If you see text on the screen when you type, you are in Edit mode.

  6. Type :w and press Return to write the text to the file you created. At the bottom of the vi window, you will see confirmation that the text has been written to the file (see Figure 9.8).

    Figure 9.8. The message at the bottom of this vi window indicates that one line of text has been written to the file newtest.txt.


  7. To continue adding text to the file, type a. The command line disappears and the cursor becomes active after the last text you entered.

  8. Continue adding text and writing it to the file.

  9. When you are done, press Esc to enter Command mode; then type :q and press Return to quit vi. You will return to the command line.


To save long manual pages for a command, use the man command on that command and press the spacebar to reveal the entire text of the manual pages. Select the manual text you want to save in a file and select File, Save Selected Text As. Name the text file and save it. You can then refer to that file when you need help with that command.

Because GUI text editors are available, you might not want to use Unix text editors such as vi, but for short, plain-text documents, such as a Shell script, these editors can be useful.

To edit an existing file with vi, type vi filename, where filename is the name of the file you want to edit, and press Return. The file opens and you can begin editing it.

If you intend to use vi, make sure that you read its manual pages in detail; vi has many commands available, but they are hard to figure out without help.

Compressing, Uncompressing, and Extracting Files

Unix has some built-in programs to enable you to work with compressed files, including

  • To compress a file, type compress filename. The file named filename is compressed and a .Z is appended to its name.

  • To uncompress a file, type uncompress filename, where filename is the name of the compressed file. The file is uncompressed.

  • You can also use the gzip compression application by typing gzip filename. Uncompress the file using the gunzip command. gzip offers various options; check its manual pages to see them.

Many Unix files are archived in the tar (tape archive) format before they are compressed. After you compress such files, you will see a file that has the .tar extension. You can extract a tar file using the command tar xvf filename, where filename is the name of the tar file.


Tar also has various options; check its manual pages for help.

    Part I: Mac OS X: Exploring the Core
    Part III: Mac OS X: Living the Digital Life