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.
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:
Type man vi. Read the manual pages to get an idea of how vi works.
Open a new Terminal window (-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.
Type i to enter Insert mode.
Type your text.
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.
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).
To continue adding text to the file, type a. The command line disappears and the cursor becomes active after the last text you entered.
Continue adding text and writing it to the file.
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.
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.