Hack 51 Editing Special Unix Files


Special Unix files need special handling. You can't simply edit them in Word and expect things to work. Here's a crash course in editing using the pico command-line editor and TextEdit GUI editor.

You've no doubt discovered OS X's default text editor, the aptly named TextEdit. Hopefully, you've also heard of and downloaded the outstanding BBEdit (http://www.bbedit.com/index.html), favorite text editor of generations of Mac users. But unless you're a Unix jock, you probably don't know that OS X ships with several other feature- and history-rich Terminal-based text editors. Veterans will tend to swear by either vi (the Visual Editor) or Emacs, but seldom both. Then there's pico, the simplest of the three, yet still more than sufficient for most simple editing work.

Here we'll provide a crash course in editing those special Unix files we talk about in this book: httpd.conf, /etc/inetd.conf, plist files, and the like. We'll skip the two with the steepest learning curve ? vi and Emacs ? and stick with pico and TextEdit.

51.1 Using pico

pico was developed at the University of Washington. It is a simple but powerful Unix text editor. To fire up pico, type pico (by itself or followed by a particular file to edit) in a Terminal [Hack #48] window (see Figure 5-14).

Figure 5-14. The pico interface

pico's interface, while perhaps a little Unixey for the uninitiated, is pretty straightforward. Rather than clicking buttons in menu bars, commands are issued by typing Control-character shortcuts; the bottom two rows provide a list of commonly used shortcuts. That ^ character prefixing all of the shortcuts stands for the Control key on your keyboard; thus, ^G signifies that for more comprehensive help, you should press Control-G (see Figure 5-15).

Figure 5-15. Getting help in pico

Press Control-X to leave pico help.

Now that you have pico warmed up, let's take it for a spin with some common operations.

51.1.1 Moving about

Move about within the text file you're editing, as you might expect, using the arrow keys. Beyond basic character-by-character movement, however, your old habits will fail you. None of the figs/command.gif- sequences work here. To scroll through long text, you cannot use the Page Up and Page Down keys on your keyboard; it's Control-Y for page up and Control-V for page down. To jump to the beginning of a line, press Control-A. To jump to the end, type Control-E.

To search within the current file for a snippet of text, press Control-W, enter the text to find at the Search: prompt, and press Return. To search for the same text again, press Control-W, followed by Return. To change your mind and cancel a search, press Control-C.

51.1.2 Saving

To save a file (see Figure 5-16), press figs/command.gif-O (write out ? go figure!).

Figure 5-16. Saving a file in pico

Type a filename or fully qualified path (e.g., /tmp/test.txt) to which to save, as shown in Figure 5-17, and press Return.

Figure 5-17. Supplying a filename or path to save to

You can also use the built-in file and directory browser (press Control-T) to locate a particular directory into which to save your file (see Figure 5-18). Use the arrow keys to move about, Return to move into a directory, .. to move up a directory, and e to select a directory and return to the File Name to write: prompt. You can also select a filename, and whatever you save will overwrite what's already there.

Figure 5-18. The pico directory browser
51.1.3 Opening

Oddly enough, pico doesn't have an Open File command. Instead, you insert the contents of a file into the editor, as shown in Figure 5-19. Press Control-R and everything's pretty much the same as it was with saving, directory browser and all. The only difference is that you use Return rather then e to make your final selection. The selected file's contents will appear in the editor, appended to anything you've already been editing.

Figure 5-19. Inserting a file
51.1.4 Selecting text

Selecting a block of text in pico is not as straightforward as using your mouse. In fact, the mouse is utterly useless in pico and just about every other command-line application.

To select a block of text, use the arrow keys to position your cursor at the start of the text you want to select and press Control-Shift-6 (a.k.a. Control-^). pico will respond with [ Mark Set ]. Move about until you've selected all the text you wish; selected text is called out in inverse colors.

To simply unselect the text, press Control-Shift-6 again.

To cut the selected block, press Control-K. To paste it somewhere, move the cursor to the right place and press Control-U. Note that there's no copy in pico. To copy, just cut and paste (Control-K, Control-U) in place and then paste again with Control-U anywhere and as many times as you wish.

51.1.5 Deleting

Use your Delete key as usual to delete the character before your cursor. To delete the character after the cursor, press Control-D. Delete an entire line with Control-K.

51.1.6 Leaving

To get out of pico at any time, press Control-X. If you've not saved what you're currently editing, pico will offer you one last chance to Save modified buffer.

51.2 TextEdit

TextEdit (Applications TextEdit) is the default GUI text editor. Being more like any other application you've used than pico, TextEdit is also much more novice-friendly.

As in most Mac applications, you drag and drop selections made with your mouse. Saving, opening, cutting, copying, and pasting work as expected. Moving about with the arrow keys and figs/command.gif-modified arrow keys also holds no surprises. Page Up and Page Down shift up and down a page.

51.3 Setting Your Default Command-Line Editor

The default command-line editor is vi ? not a great choice for beginners. Commands like crontab -e [Hack #53] use the default as their editor of choice, rather than allowing you to use the pico editor you now know and love.

To set your command-line editor of choice to pico, create a file in your home directory called .tcshrc containing the following single line:

setenv EDITOR /usr/bin/pico

The next time you invoke a Terminal command that requires a default editor (and respects the EDITOR environment variable), pico will be used instead of vi.

51.4 Why Not Simply Go GUI?

Why, then, would anyone bother delving into antiquity with pico or any of the other Terminal-based editors? Good question.

One answer is that editing on the command line is far better integrated into working with special Unix files than throwing them out to a GUI text editor and jumping back to the command line when you're done. This is especially true when it comes to editing files you don't have permission to edit without becoming an administrative (root) user [Hack #50]. Just type sudo pico special_filename, authenticate yourself, and you're editing. Try opening that same file in TextEdit and you won't be allowed to save it once you're done editing.

That said, you can invoke TextEdit as the administrative user from the command line:

% sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit

You have added a step, though, since you'll still have to open the particular file you were after from within TextEdit.

Another reason to use the command line is that some Terminal commands automatically open your default editor for you and rely upon knowing when it's done. This doesn't always work particularly well with an external GUI editor like TextEdit.

For those who regularly log in to a remote machine [Hack #71] for administrative tasks, there's no choice but one of the Terminal-based editors ? either that or running a desktop-sharing app, which is overkill for editing a configuration file.

And, as mentioned, vi and Emacs are powerful editors, enabling far more than would be possible within a traditional text editor. This power takes some know-how, but it quickly becomes indispensable.

The choice is really yours. If you're more comfortable using a GUI editor like TextEdit or BBEdit (which can authenticate as an administrative user from the GUI), go right ahead. If you're using the command line quite a bit and it's starting to grow on you, pico, vi, or Emacs might turn out to be your killer editor.

?Wei-Meng Lee