Escaping The Cat Dimension Mac OS

A graphics context represents a drawing destination. It contains drawing parameters and all device-specific information that the drawing system needs to perform any subsequent drawing commands. A graphics context defines basic drawing attributes such as the colors to use when drawing, the clipping area, line width and style information, font information, compositing options, and several others.

You can obtain a graphics context by using Quartz context creation functions or by using higher-level functions provided by one of the Mac OS X frameworks or the UIKit framework in iOS. Quartz provides functions for various flavors of Quartz graphics contexts including bitmap and PDF, which you can use to create custom content.

The all NEW sequel to Myst and Riven new technology, new story and a new arch enemy. It's the perfect place to plan revenge. The success of Myst continues with 5 entirely new ages to explore and a. The Macintosh (mainly Mac since 1998) is a family of personal computers designed, manufactured, and sold by Apple Inc. Since January 1984. The original Macintosh is the first successful mass-market personal computer to have featured a graphical user interface, built-in screen, and mouse. Apple sold the Macintosh alongside its popular Apple II, Apple III, and Apple Lisa families of computers. In short, escape sequences can adversely change the way how we see things on the terminal. And it can have really bad consequences: And it works everywhere and with anything! For instance, here’s an example of escape injection in a Python script on Mac OS.

Cat

This chapter shows you how to create a graphics context for a variety of drawing destinations. A graphics context is represented in your code by the data type CGContextRef, which is an opaque data type. After you obtain a graphics context, you can use Quartz 2D functions to draw to the context, perform operations (such as translations) on the context, and change graphics state parameters, such as line width and fill color.

Drawing to a View Graphics Context in iOS

To draw to the screen in an iOS application, you set up a UIView object and implement its drawRect: method to perform drawing. The view’s drawRect: method is called when the view is visible onscreen and its contents need updating. Before calling your custom drawRect: method, the view object automatically configures its drawing environment so that your code can start drawing immediately. As part of this configuration, the UIView object creates a graphics context (a CGContextRef opaque type) for the current drawing environment. You obtain this graphics context in your drawRect: method by calling the UIKit function UIGraphicsGetCurrentContext.

The default coordinate system used throughout UIKit is different from the coordinate system used by Quartz. In UIKit, the origin is in the upper-left corner, with the positive-y value pointing downward. The UIView object modifies the CTM of the Quartz graphics context to match the UIKit conventions by translating the origin to the upper left corner of the view and inverting the y-axis by multiplying it by -1. For more information on modified-coordinate systems and the implications in your own drawing code, see Quartz 2D Coordinate Systems.

UIView objects are described in detail in View Programming Guide for iOS.

Creating a Window Graphics Context in Mac OS X

When drawing in Mac OS X, you need to create a window graphics context that’s appropriate for the framework you are using. The Quartz 2D API itself provides no functions to obtain a windows graphics context. Instead, you use the Cocoa framework to obtain a context for a window created in Cocoa.

You obtain a Quartz graphics context from within the drawRect: routine of a Cocoa application using the following line of code:

The method currentContext returns the NSGraphicsContext instance of the current thread. The method graphicsPort returns the low-level, platform-specific graphics context represented by the receiver, which is a Quartz graphics context. (Don’t get confused by the method names; they are historical.) For more information see NSGraphicsContext Class Reference.

After you obtain the graphics context, you can call any of the Quartz 2D drawing functions in your Cocoa application. You can also mix Quartz 2D calls with Cocoa drawing calls. You can see an example of Quartz 2D drawing to a Cocoa view by looking at Figure 2-1. The drawing consists of two overlapping rectangles, an opaque red one and a partially transparent blue one. You’ll learn more about transparency in Color and Color Spaces. The ability to control how much you can “see through” colors is one of the hallmark features of Quartz 2D.

To create the drawing in Figure 2-1, you first create a Cocoa application Xcode project. In Interface Builder, drag a Custom View to the window and subclass it. Then write an implementation for the subclassed view, similar to what Listing 2-1 shows. For this example, the subclassed view is named MyQuartzView. The drawRect: method for the view contains all the Quartz drawing code. A detailed explanation for each numbered line of code appears following the listing.

Note: The drawRect: method of the NSView class is invoked automatically each time the view needs to be drawn. To find out more about overriding the drawRect: method, see NSView Class Reference.

Listing 2-1 Drawing to a window graphics context

Here’s what the code does:

  1. Obtains a graphics context for the view.

  2. This is where you insert your drawing code. The four lines of code that follow are examples of using Quartz 2D functions.

  3. Sets a red fill color that’s fully opaque. For information on colors and alpha (which sets opacity), see Color and Color Spaces.

  4. Fills a rectangle whose origin is (0,0) and whose width is 200 and height is 100. For information on drawing rectangles, see Paths.

  5. Sets a blue fill color that’s partially transparent.

  6. Fills a rectangle whose origin is (0,0) and whose width is 100 and height is 200.

Creating a PDF Graphics Context

When you create a PDF graphics context and draw to that context, Quartz records your drawing as a series of PDF drawing commands written to a file. You supply a location for the PDF output and a default media box—a rectangle that specifies bounds of the page. Figure 2-2 shows the result of drawing to a PDF graphics context and then opening the resulting PDF in Preview.

The Quartz 2D API provides two functions that create a PDF graphics context:

  • CGPDFContextCreateWithURL, which you use when you want to specify the location for the PDF output as a Core Foundation URL. Listing 2-2 shows how to use this function to create a PDF graphics context.

  • CGPDFContextCreate, which you use when you want the PDF output sent to a data consumer. (For more information see Data Management in Quartz 2D.) Listing 2-3 shows how to use this function to create a PDF graphics context.

A detailed explanation for each numbered line of code follows each listing.

iOS Note: A PDF graphics context in iOS uses the default coordinate system provided by Quartz, without applying a transform to match the UIKit coordinate system. If your application plans on sharing drawing code between your PDF graphics context and the graphics context provided by UIView object, your application should modify the CTM of the PDF graphics context to modify the coordinate system. See Quartz 2D Coordinate Systems.

Listing 2-2 Calling CGPDFContextCreateWithURL to create a PDF graphics context

Here’s what the code does:

  1. Calls the Core Foundation function to create a CFURL object from the CFString object supplied to the MyPDFContextCreate function. You pass NULL as the first parameter to use the default allocator. You also need to specify a path style, which for this example is a POSIX-style pathname.

  2. Calls the Quartz 2D function to create a PDF graphics context using the PDF location just created (as a CFURL object) and a rectangle that specifies the bounds of the PDF. The rectangle (CGRect) was passed to the MyPDFContextCreate function and is the default page media bounding box for the PDF.

  3. Releases the CFURL object.

  4. Returns the PDF graphics context. The caller must release the graphics context when it is no longer needed.

Listing 2-3 Calling CGPDFContextCreate to create a PDF graphics context

Here’s what the code does:

  1. Calls the Core Foundation function to create a CFURL object from the CFString object supplied to the MyPDFContextCreate function. You pass NULL as the first parameter to use the default allocator. You also need to specify a path style, which for this example is a POSIX-style pathname.

  2. Creates a Quartz data consumer object using the CFURL object. If you don’t want to use a CFURL object (for example, you want to place the PDF data in a location that can’t be specified by a CFURL object), you can instead create a data consumer from a set of callback functions that you implement in your application. For more information, see Data Management in Quartz 2D.

  3. Calls the Quartz 2D function to create a PDF graphics context passing as parameters the data consumer and the rectangle (of type CGRect) that was passed to the MyPDFContextCreate function. This rectangle is the default page media bounding box for the PDF.

  4. Releases the data consumer.

  5. Releases the CFURL object.

  6. Returns the PDF graphics context. The caller must release the graphics context when it is no longer needed.

Listing 2-4 shows how to call the MyPDFContextCreate routine and draw to it. A detailed explanation for each numbered line of code appears following the listing.

Listing 2-4 Drawing to a PDF graphics context

Escaping The Cat Dimension Mac Os 11

Here’s what the code does:

  1. Declares a variable for the rectangle that you use to define the PDF media box.

  2. Sets the origin of the media box to (0,0) and the width and height to variables supplied by the application.

  3. Calls the function MyPDFContextCreate (See Listing 2-3) to obtain a PDF graphics context, supplying a media box and a pathname. The macro CFSTR converts a string to a CFStringRef data type.

  4. Sets up a dictionary with the page options. In this example, only the media box is specified. You don’t have to pass the same rectangle you used to set up the PDF graphics context. The media box you add here supersedes the rectangle you pass to set up the PDF graphics context.

  5. Signals the start of a page. This function is used for page-oriented graphics, which is what PDF drawing is.

  6. Calls Quartz 2D drawing functions. You replace this and the following four lines of code with the drawing code appropriate for your application.

  7. Signals the end of the PDF page.

  8. Releases the dictionary and the PDF graphics context when they are no longer needed.

You can write any content to a PDF that’s appropriate for your application—images, text, path drawing—and you can add links and encryption. For more information see PDF Document Creation, Viewing, and Transforming.

Creating a Bitmap Graphics Context

A bitmap graphics context accepts a pointer to a memory buffer that contains storage space for the bitmap. When you paint into the bitmap graphics context, the buffer is updated. After you release the graphics context, you have a fully updated bitmap in the pixel format you specify.

Note: Bitmap graphics contexts are sometimes used for drawing offscreen. Before you decide to use a bitmap graphics context for this purpose, see Core Graphics Layer Drawing. CGLayer objects (CGLayerRef) are optimized for offscreen drawing because, whenever possible, Quartz caches layers on the video card.

iOS Note: iOS applications should use the function UIGraphicsBeginImageContextWithOptions instead of using the low-level Quartz functions described here. If your application creates an offscreen bitmap using Quartz, the coordinate system used by bitmap graphics context is the default Quartz coordinate system. In contrast, if your application creates an image context by calling the function UIGraphicsBeginImageContextWithOptions, UIKit applies the same transformation to the context’s coordinate system as it does to a UIView object’s graphics context. This allows your application to use the same drawing code for either without having to worry about different coordinate systems. Although your application can manually adjust the coordinate transformation matrix to achieve the correct results, in practice, there is no performance benefit to doing so.

You use the function CGBitmapContextCreate to create a bitmap graphics context. This function takes the following parameters:

  • data. Supply a pointer to the destination in memory where you want the drawing rendered. The size of this memory block should be at least (bytesPerRow*height) bytes.

  • width. Specify the width, in pixels, of the bitmap.

  • height. Specify the height, in pixels, of the bitmap.

  • bitsPerComponent. Specify the number of bits to use for each component of a pixel in memory. For example, for a 32-bit pixel format and an RGB color space, you would specify a value of 8 bits per component. See Supported Pixel Formats.

  • bytesPerRow. Specify the number of bytes of memory to use per row of the bitmap.

    Tip: When you create a bitmap graphics context, you’ll get the best performance if you make sure the data and bytesPerRow are 16-byte aligned.

  • colorspace. The color space to use for the bitmap context. You can provide a Gray, RGB, CMYK, or NULL color space when you create a bitmap graphics context. For detailed information on color spaces and color management principles, see Color Management Overview. For information on creating and using color spaces in Quartz, see Color and Color Spaces. For information about supported color spaces, see Color Spaces and Bitmap Layout in the Bitmap Images and Image Masks chapter.

  • bitmapInfo. Bitmap layout information, expressed as a CGBitmapInfo constant, that specifies whether the bitmap should contain an alpha component, the relative location of the alpha component (if there is one) in a pixel, whether the alpha component is premultiplied, and whether the color components are integer or floating-point values. For detailed information on what these constants are, when each is used, and Quartz-supported pixel formats for bitmap graphics contexts and images, see Color Spaces and Bitmap Layout in the Bitmap Images and Image Masks chapter.

Listing 2-5 shows how to create a bitmap graphics context. When you draw into the resulting bitmap graphics context, Quartz records your drawing as bitmap data in the specified block of memory. A detailed explanation for each numbered line of code follows the listing.

Listing 2-5 Creating a bitmap graphics context

Here’s what the code does:

  1. Declares a variable to represent the number of bytes per row. Each pixel in the bitmap in this example is represented by 4 bytes; 8 bits each of red, green, blue, and alpha.

  2. Creates a generic RGB color space. You can also create a CMYK color space. See Color and Color Spaces for more information and for a discussion of generic color spaces versus device dependent ones.

  3. Calls the calloc function to create and clear a block of memory in which to store the bitmap data. This example creates a 32-bit RGBA bitmap (that is, an array with 32 bits per pixel, each pixel containing 8 bits each of red, green, blue, and alpha information). Each pixel in the bitmap occupies 4 bytes of memory. In Mac OS X 10.6 and iOS 4, this step can be omitted—if you pass NULL as bitmap data, Quartz automatically allocates space for the bitmap.

  4. Creates a bitmap graphics context, supplying the bitmap data, the width and height of the bitmap, the number of bits per component, the bytes per row, the color space, and a constant that specifies whether the bitmap should contain an alpha channel and its relative location in a pixel. The constant kCGImageAlphaPremultipliedLast indicates that the alpha component is stored in the last byte of each pixel and that the color components have already been multiplied by this alpha value. See The Alpha Value for more information on premultiplied alpha.

  5. If the context isn’t created for some reason, frees the memory allocated for the bitmap data.

  6. Releases the color space.

  7. Returns the bitmap graphics context. The caller must release the graphics context when it is no longer needed.

Listing 2-6 shows code that calls MyCreateBitmapContext to create a bitmap graphics context, uses the bitmap graphics context to create a CGImage object, then draws the resulting image to a window graphics context. Figure 2-3 shows the image drawn to the window. A detailed explanation for each numbered line of code follows the listing.

Listing 2-6 Drawing to a bitmap graphics context

Here’s what the code does:

  1. Declares a variable to store the origin and dimensions of the bounding box into which Quartz will draw an image created from the bitmap graphics context.

  2. Sets the origin of the bounding box to (0,0) and the width and height to variables previously declared, but whose declaration are not shown in this code.

  3. Calls the application-supplied function MyCreateBitmapContext (see Listing 2-5) to create a bitmap context that is 400 pixels wide and 300 pixels high. You can create a bitmap graphics context using any dimensions that are appropriate for your application.

  4. Calls Quartz 2D functions to draw into the bitmap graphics context. You would replace this and the next four lines of code with drawing code appropriate for your application.

  5. Creates a Quartz 2D image (CGImageRef) from the bitmap graphics context.

  6. Draws the image into the location in the window graphics context that is specified by the bounding box. The bounding box specifies the location and dimensions in user space in which to draw the image.

    This example does not show the creation of the window graphics context. See Creating a Window Graphics Context in Mac OS X for information on how to create one.

  7. Gets the bitmap data associated with the bitmap graphics context.

  8. Releases the bitmap graphics context when it is no longer needed.

  9. Free the bitmap data if it exists.

  10. Releases the image when it is no longer needed.

Supported Pixel Formats

Table 2-1 summarizes the pixel formats that are supported for bitmap graphics context, the associated color space (cs), and the version of Mac OS X in which the format was first available. The pixel format is specified as bits per pixel (bpp) and bits per component (bpc). The table also includes the bitmap information constant associated with that pixel format. See CGImage Reference for details on what each of the bitmap information format constants represent.

Table 2-1 Pixel formats supported for bitmap graphics contexts

CS

Pixel format and bitmap information constant

Availability

Null

8 bpp, 8 bpc, kCGImageAlphaOnly

Mac OS X, iOS

Gray

8 bpp, 8 bpc,kCGImageAlphaNone

Mac OS X, iOS

Gray

8 bpp, 8 bpc,kCGImageAlphaOnly

Mac OS X, iOS

Gray

16 bpp, 16 bpc, kCGImageAlphaNone

Mac OS X

Gray

32 bpp, 32 bpc, kCGImageAlphaNonekCGBitmapFloatComponents

Mac OS X

RGB

16 bpp, 5 bpc, kCGImageAlphaNoneSkipFirst

Mac OS X, iOS

RGB

32 bpp, 8 bpc, kCGImageAlphaNoneSkipFirst

Mac OS X, iOS

RGB

32 bpp, 8 bpc, kCGImageAlphaNoneSkipLast

Mac OS X, iOS

RGB

32 bpp, 8 bpc, kCGImageAlphaPremultipliedFirst

Mac OS X, iOS

RGB

32 bpp, 8 bpc, kCGImageAlphaPremultipliedLast

Mac OS X, iOS

RGB

64 bpp, 16 bpc, kCGImageAlphaPremultipliedLast

Mac OS X

RGB

64 bpp, 16 bpc, kCGImageAlphaNoneSkipLast

Mac OS X

RGB

128 bpp, 32 bpc, kCGImageAlphaNoneSkipLastkCGBitmapFloatComponents

Mac OS X

RGB

128 bpp, 32 bpc, kCGImageAlphaPremultipliedLastkCGBitmapFloatComponents

Mac OS X

CMYK

32 bpp, 8 bpc, kCGImageAlphaNone

Mac OS X

CMYK

64 bpp, 16 bpc, kCGImageAlphaNone

Mac OS X

CMYK

128 bpp, 32 bpc, kCGImageAlphaNonekCGBitmapFloatComponents

Mac OS X

Anti-Aliasing

Bitmap graphics contexts support anti-aliasing, which is the process of artificially correcting the jagged (or aliased) edges you sometimes see in bitmap images when text or shapes are drawn. These jagged edges occur when the resolution of the bitmap is significantly lower than the resolution of your eyes. To make objects appear smooth in the bitmap, Quartz uses different colors for the pixels that surround the outline of the shape. By blending the colors in this way, the shape appears smooth. You can see the effect of using anti-aliasing in Figure 2-4. You can turn anti-aliasing off for a particular bitmap graphics context by calling the function CGContextSetShouldAntialias. The anti-aliasing setting is part of the graphics state.

You can control whether to allow anti-aliasing for a particular graphics context by using the function CGContextSetAllowsAntialiasing. Pass true to this function to allow anti-aliasing; false not to allow it. This setting is not part of the graphics state. Quartz performs anti-aliasing when the context and the graphic state settings are set to true.

Obtaining a Graphics Context for Printing

Cocoa applications in Mac OS X implement printing through custom NSView subclasses. A view is told to print by invoking its print: method. The view then creates a graphics context that targets a printer and calls its drawRect: method. Your application uses the same drawing code to draw to the printer that it uses to draw to the screen. It can also customize the drawRect: call to an image to the printer that is different from the one sent to the screen.

For a detailed discussion of printing in Cocoa, see Printing Programming Guide for Mac.



Escaping The Cat Dimension Mac Os X

Copyright © 2001, 2017 Apple Inc. All Rights Reserved. Terms of Use Privacy Policy Updated: 2017-03-21

Escaping The Cat Dimension Mac Os Catalina

Mac OS X Command Line 101
by Richard Burton

Command Line Text Editing Basics
Part XII of this series...
July 26th, 2002

This method answers the purpose for which it was devised; it saves lazy editors from working and stupid editors from thinking.
-
A.E. Housman, 'The Editing of Manilus'

This series is designed to help you learn more about the Mac OS X command line. If you have any questions about what you read here, check out the earlier columns, write back in the comments below, or join us in the Hardcore X! forum.

So far we have done quite a bit with manipulating files and examining information on them: their contents, properties, and so on. However, we have not yet discussed creating a file from scratch. True, we have shown how to create a file by redirecting Standard Output, and while this is powerful, it is not always the most convenient way to create all files. To do this, we need to know how to use an editor, so we will now learn the basics of vi.

Now before we start, the emacs users must pipe down and quit lobbing rotten vegetables at the stage. For those of you who are new to Unix and the command line, the vi vs. emacs debate may have been the closest thing to a holy war that the Unix community has ever seen. Each side's adherents extol the virtue of their respective editor of choice and some feel the need to denigrate the other. This is complete eyewash, of course, as each editor has its virtues. I've chosen vi for four reasons. First, it isn't as intimidating for a newcomer; emacs has a plethora of commands, while vi requires less to learn up front and doesn't present options that could be confusing to a newbie. Second, there are some Unix systems which still don't come with emacs or is not installed by the sysadmin, while vi is ubiquitous; this is hard to believe, but true. Third, Ye Editor probably wouldn't pay me the same huge commission for all the articles that would be required to cover emacs. And fourth, I am using vi to write these columns as an example of what can be done from the command line. Besides, we all know that vi people are superior humans, so phblt! to the emacs lusers.

vi is based on the venerable ex editor, which is now more or less the Unix equivalent of the horse and buggy; it gets you there, but not what you want to use unless you must. Still, ex did have its points, and vi will let you run those useful ex commands; more on that later. vi has the advantage of being, get this, a full screen editor which lets you look at more than one line at a time. This seems like no big deal, but when vi premiered, it was amazing. (Or so I'm told; I haven't been computing that long.) And in the grand tradition of Unix utilities, vi makes simple things simple and complex things possible.

To create a file with vi, simply type:

In this column, we will step through some of the basics so you can get started with vi. At the command line, type:
This should clear your terminal screen and give you something that looks like:
You may be wondering what's with all the tildes (~). Remember that vi is a full screen editor. However, when you give it the name of a file that doesn't exist, or a file with only a few lines, you can have more lines on the screen than lines in the file. To denote this, vi will indicate 'this is beyond the end of the file, pal' with a tilde at the beginning of a line on the screen.

vi has two modes: a command mode and an input mode. When it starts, you are in command mode. To enter input mode (i.e. to insert text), just type the letter 'i' and start typing. In your terminal, type and 'i' and add the following. (Don't worry if you mistype something, I'll show you how to replace things in a bit.)

When you have finished typing, hit the ESCAPE key (upper left of the keyboard) to go back to command mode; your cursor should be over the 'e' in Wilde. In fact, if you are in vi and you can't remember whether you are in command mode or input mode, hitting ESCAPE will always take you out of input mode; if you are already in command mode, you hear an error beep, but no harm is done.

You may well have made mistakes when typing that. After all, we're only human. Well, you are. One of the easiest way to reverse anything you wanted to do is with the undo command, 'u'. This will put things back to the way they were before your previous change. (Moving around does not count as a change.) This simple little command has saved my bacon more than once; keep it in mind.

If you are in command mode, you can move one space left, right, up, or down using the arrow keys. Yes, it's obvious and it should go without saying; that's why I said it. Back in the day, however, you couldn't count on having these keys on your keyboard or having them mapped the right way. In old Unix utilities, 'h', 'j', 'k', and 'l' do this: 'h' for left, 'j' for down, 'k' for up, and 'l' for right. (Anyone who plays nethack will find that familiar.) In fact, I half-recommend you use these letters; if you ever wind up on another Unix box, the keyboard may have the arrows set up a bit differently and you'll need to use hjkl. Take a few second to move around and get familiar with them.

Now, using hjkl can get the job done, but they can be tiresome, particularly if you have to traverse a whole line. Fortunately, there are some shortcuts to promote the virtue of laziness. First, vi will let you run almost any command repeatedly by prepending a number to it. (These commands are said to 'take a count' in the vernacular; I don't know if that's an official term.) Go all the way back to the beginning of the first line. Say you want to move eight characters to the right. Instead of typing 'l' eight times, you can simply type '8l' to do the same thing. Try this; it should take your cursor to the 'h' in 'the'. '3j' will then take you to the 't' in 'out'.

Also, notice the line that starts 'This is a very long ...' Assuming that you didn't hit RETURN after the 'nor' in 'normally', vi wrapped the line on the screen, but it still considers it one line. Trying using 'j' and 'k' to move up and down through the line. You originally typed this as one line, so vi stores it internally as one line. That's why you may see the cursor moving through what seems to be more than one line on one 'j' or 'k'.

In addition, there are other handy movement commands. A '0' (zero, not a capital 'o') will take you to the beginning of your current line; a '$' will take you to the end. A 'w' will advance you to the beginning of the next word; and yes, '3w' will advance you three words. Likewise, the 'b' will move you back one word; '2b' will take you back two. And if you want to advance to the end of a word, use the 'e' command, which also can take a count.

So now we can fly around our test file with the greatest of ease. However, we still need to change things. If you forgot a letter, you can of course use the 'i' command to insert it. (Don't forget the ESCAPE.) The 'x' command will delete the character under the cursor. 'x' also takes a count, like the movement commands. '5x' will delete the character under the cursor, plus the next four (for a total of five). 'r' will replace the character under the cursor with the next character you type. Let's say you typed 'them' when you meant to type 'then'. You can just place your cursor over the 'm', then type rn and the word is fixed. Now, having said that, I must warn you. 'r' takes a count, but it may not do what you think. If you type 3rn the next three characters will all be replaced with 'n'. If you would like to replace, say, 'and' with 'but', use the 'R' command. It will overwrite characters as long as you type; to escape from this form of input mode, you should, of course, hit ESCAPE.

Two other commands deserve attention: the delete and change commands. Delete, 'd', has two forms. You can use it to delete a word, 'dw', or to delete a whole line, 'dd'. Both of these forms can take a count, as can both forms of the change command, 'c'. To change a word, use 'cw'; to change a whole line, use 'cc'. And as you can probably guess, once you are done typing in the change, you hit ESCAPE to get back to command mode.

With these commands, you can now get to anywhere in your file and correct any errors you made. Why don't you go ahead and do this? It will give you some nice practice, so even if you did type it properly, add a couple of mistakes, then change them back. Remember: ESCAPE takes you out of insert mode and into command mode. So toddle along; we'll wait.

.
.
.

Ah, back so soon? [HIDES ADULT BEVERAGE] Splendid. Capital. Now there is one thing we must do: save the file. vi offers many different ways to do this, depending on just what you want to do. If you want to save the file and leave vi, the 'ZZ' command will do just that. However, if you want to continue editing after saving the file, just type ':w' while in command mode. If you wish to quit without saving your changes, use the ':q' command. (For those who are wondering, typing a colon while in command mode will allow you to enter an ex command. The reasoning here, I think, is that ex provided good commands to do some things already, whether the editor was a line editor like ex or a full-screen editor like vi, so vi just lets the commands it could inherit from ex work without vi's author(s) having to rewrite code.)

So, there you have the basics of vi. That isn't everything, of course. In the next column, we'll examine vi in more depth.

You are encouraged to send Richard your comments, or to post them below.

Most Recent Mac OS X Command Line 101 Columns

Command Line History & Editing Your Commands
November 22nd

Pico: An Easy To Use Command Line Editor
November 1st

Understanding The 'grep' Command In Mac OS X
October 4th

Command Line History & Editing Your Commands
September 6th

Mac OS X Command Line 101 Archives

Back to The Mac Observer For More Mac News!

Richard Burton is a longtime Unix programmer and a handsome brute. He spends his spare time yelling at the television during Colts and Pacers games, writing politically incorrect short stories, and trying to shoot the neighbor's cat (not really) nesting in his garage. He can be seen running roughshod over the TMO forums under the alias tbone1.