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; what it is most useful for is 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 to.
The vi program has two modes: Edit and Command. In the Edit mode, you can enter and edit text. In the Command mode, you issue commands to the program.
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 will open, the file will be created in the current directory, and you will 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 the Edit mode.
Type your text.
Press Esc to move into the Command mode. While you are in the 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 the Command mode, the cursor will appear to jump back a couple of spaces and the bottom line of the window will be empty. You can then type a command. If you see text on the screen when you type, you are in the 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, press a. The command line will disappear and the cursor will become active after the last text you entered.
Continue adding text and writing it to the file.
When you are done, press Esc to enter the Command mode and 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 use the spacebar to reveal the entire text of the manual pages. Select the manual text you want to save in a file and choose 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 you have GUI text editors 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 will open 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 it is unlikely that you will be able to figure them out without help.
Unix has some "built-in" programs to enable you to work with compressed files.
To compress a file, type compress filename. The file named filename will be compressed and a .Z will be appended to its name.
To uncompress a file, type uncompress filename, where filename is the name of the compressed file. The file will be 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.