05.01.2013 Views

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

Mac OS X Leopard - ARCAism

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

312<br />

CHAPTER 18 INTRODUCING DARWIN AND THE SHELL<br />

Editing Files<br />

Editing files in Darwin is just one of those things that you’ll eventually have to do to get the most<br />

out of it. For the most part, using a normal <strong>Mac</strong> <strong>OS</strong> X text editor like TextMate (€39), BBEdit<br />

($125), TextWrangler (free), or Smultron (free, donations accepted) may be your best bet—<br />

however, at times it may be handy or necessary to edit a file on the command line. <strong>Leopard</strong> ships<br />

with four command line–based text editors in Darwin: Ed, Vim, Emacs, and Nano. Ed is the<br />

original text editor for UNIX, but it has been surpassed in both usability and features by many<br />

newer text editors. Despite this, it is still included in most UNIX distributions since it has found<br />

a small niche within certain shell scripts. Each of the remaining three has its advantages, and<br />

what it comes down to for most people is a matter of taste and habit (i.e., once you start using<br />

one it becomes a bit frustrating to use another, since they vary quite a bit).<br />

Vim<br />

NOTE Since we mentioned some GUI text editors previously, it’s worth noting that many of<br />

these come with a command-line executable as well, so that you may open any file in them<br />

from the command line. Specifically, BBEdit allows you to install the bbedit command-line executable,<br />

and TextMate allows you to install the tm command-line executable. Each of these will<br />

open the file in the GUI application, but it makes it handy to do so from the command line.<br />

Also . . . obviously these won’t help you much if you are accessing the machine remotely<br />

through a terminal window.<br />

Vim is, as the name suggests, an “improved” clone of Vi (Visual Editor), which in turn is based on<br />

Ex, which is based on the aforementioned Ed (which was based on an even older text editor<br />

called QED). Vim is interesting to people new to command-line editors in that, like its predecessors,<br />

it is a dual-mode text editor. To get things done, you must switch between an insert or edit<br />

mode and a command mode. While this way of working with a text editor takes some getting<br />

used to, it’s fairly easy once you get the knack of it. Also, Vim, Vi, or one of its clones is installed<br />

by default on almost every version of UNIX or Linux, so once you learn it, you can count on<br />

something like it to be installed on any UNIX (or UNIX-like) system you encounter.<br />

NOTE Vim specifically has a special third mode—visual mode—which can be toggled by<br />

typing v in command mode. Once in visual mode, you can move the cursor around normally—<br />

however, the text you move over will be selected. This mode is very handy for selecting text<br />

for precise copying and pasting.<br />

When you launch Vim, you can either open an existing file, create a new file, or just jump<br />

into the editor without creating a file. The syntax is fairly straightforward:<br />

vim [filename]<br />

where filename is either an existing file or one you wish to create.<br />

NOTE In Darwin, vi, view, vimdiff, and ex all point to vim—however, they each cause Vim to<br />

exhibit different default behavior. vi and vim will both launch in normal mode, view will open a<br />

file read-only, vimdiff allows you to open multiple files at the same time so that you can compare<br />

them, and ex will open Vim in ex mode (the :vi command will return you to normal mode).<br />

Vim will begin in command mode when opening a new or existing file; so, if we open the<br />

soliloquy file we’ve been working with earlier in the chapter, we will be presented with the<br />

following:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!