Editing Text Files

While editing text files in the GUI is straightforward using the built-in TextEdit program (/Applications), editing files using the command-line tools hasn’t always been a walk in the park, unless you are comfortable with old-school text editors like vi or Emacs. But thanks to the open command, several of the command-line editing tasks that you need to perform can be done in the GUI. For example, to open your bash history file in TextEdit, use the following command:

$ open ~/.bash_history

For most text-file editing, however, TextEdit is a little underpowered. Also, it can’t be used as the command-line editor program (via the EDITOR environment variable), and it can’t be used to edit files while you are logged in remotely.

nano

If you do need to edit text files on the command line—say you are logged in remotely or need to edit a file owned by root—the easiest text editor to use on the system is nano. To use nano to edit a file, use the following command:

$ nano
               filename

When you use nano, you’ll see the user interface shown in Figure 4-5. In place of a toolbar, there is a series of commands at the bottom of the screen. The caret (^) character prefixing all those lines stands for the Control key on your keyboard. Simply press the Control key plus the letter of the command to perform some action. For example, to save a file, which nano refers to as “WriteOut,” hit Control-O. To open, or “Read” a file, hit Control-R. And to exit, hit Control-X.

Tip

Versions of Mac OS X prior to 10.4 used pico for a simple text editor. On Tiger, nano is used instead, and the command /usr/bin/pico has been replaced with a link to /usr/bin/nano.

Aside from the Control key combinations, nano is fairly straightforward to use. Arrow keys move the cursor up and down, and whatever you type is inserted wherever the cursor is located.

The nano text editor

Figure 4-5. The nano text editor

vi and Emacs

Mac OS X also ships with the two venerable Unix powerhouses of text editing: vi and Emacs. Each of these tools has a long history and feature sets that are even longer. These tools are powerful indeed and up to the task of any amount of text editing that you want to do—including hardcore development—but in many ways, they are more complicated than what you need for everyday tasks. However, if you are already a user of either of these two editors, they are available for use on the system.

Tip

In Mac OS X Panther, the default vi was switched to vim, a vi replacement that duplicates all the functionality of the original vi and extends it in many useful ways, adding unlimited undo and syntax highlighting.

This isn’t the place for a full tutorial on how to use vi or Emacs, but you should be able to exit either of these shells if you find yourself in one. To exit vi, type the following:

:q!

To exit Emacs, type Control-X, and then Control-C.

Get Running Mac OS X Tiger now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.