Section 4.5. Editing Text Files


4.5. 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.

Alternatives to TextEdit

While Mac OS X's built-in TextEdit can certainly get the job done, there are text editors that are far more robust. The venerable BBEdit (http://www.barebones.com) has been a popular text editor on the Mac for many years now. Recent versions of BBEdit have added support for script editing, allowing you to modify and run a script without having to move back and forth between the BBEdit application and the Terminal. The only downside to BBEdit is its cost: $150. However, BareBones software also offers TextWrangler , which is a lighter-weight text editor and best of all, it's free.

A newer text editor that's gained some popularity is SubEthaEdit (http://www.codingmonkeys.de). SubEthaEdit offers many of the features found in BBEdit, but touts its collaboration abilities. Using Bonjour, SubEthaEdit users can share and modify documents on the fly.

Both SubEthaEdit and BBEdit include a command-line tool for easy integration with Mac OS X's shell.


4.5.1. nano

If you do need to edit text files on the command linesay you are logged in remotely or need to edit a file owned by rootthe 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.

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.

Figure 4-5. The nano text editor


4.5.2. 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 doincluding hardcore developmentbut 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.

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.




Running Mac OS X Tiger
Running Mac OS X Tiger: A No-Compromise Power Users Guide to the Mac (Animal Guide)
ISBN: 0596009135
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net