Unix Text Editors

For years, two text editors have held equal sway in Unix culture: vi and Emacs. Not one to take sides, Mac OS X ships with both of them. The following two sections give you a whirlwind tour of both.

Tip

You don’t have to use one of these editors in order to open a text document from a Terminal window. You can instead use the open command with its -a (application-specifying) option to send the document to an Aqua text editor, such as TextEdit, like this:

% open -a TextExit 
                  somefile.txt

vi

vi is a modal editor, signifying that the meaning of what you type at any point depends entirely on what mode (context) the program is in. vi’s modes include command mode (see Section 17.4.1.2), which performs deletion, cut-and-paste, searching, and other text-editing commands, and insertion mode, which adds new text to the document through typing.

Command-line syntax

The three most common ways of starting a vi session are:

  • vi file

  • vi + line-number file

  • vi +/ pattern file

You can open file for editing, optionally at line n or at the first line matching pattern. (See Chapter 20 for more on pattern matching.) If no file is specified, vi opens with an empty buffer. See Chapter 25 for more information on command-line options for vi.

Command mode

Once the file is opened, you are in command mode. From here, you can:

  • Invoke an insert mode

  • Issue editing commands

  • Move the cursor to a different position in the file

  • Invoke ex commands

  • Invoke a Unix shell

  • Save or exit the current version of ...

Get Mac OS X in a Nutshell 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.