<<

. 17
( 69 .)



>>

DELETE A LINE OF TEXT
line.
„ Position the cursor on the
line you want to delete.



71
UNIX FOR MAC




EDIT TEXT WITH VI
:s/old/new/

Y
ou can edit text files using the multiline editing
:s/old/new/g
capabilities of the vi editor. While in Command
:%s/old/new/g
mode, you activate the command line for editing
purposes by first typing a colon (:). There are many useful The first instance replaces a single occurrence of old with
commands that you can learn about, including the ex editor new on the current line. The second instance adds the flag
that provides the line edit commands. You can obtain more g, which stands for global. This means that vi replaces all
information from the vi manual by typing man vi or occurrences of old with new on that line, and not just the
consulting online references or books about the vi editor. first occurrence. The third instance includes a range option,
%, which tells vi to replace all instances of the word
To replace text, you can use the substitute command by
throughout the entire file.
typing :s. When you type commands that begin with a colon
(:), they appear on the bottom line of the Terminal window. To join two lines of text together onto one line, you can use
vi carries out the command after you type the full command the editing command J. This removes the line break at the
and press Return. end of the current line, and joins it to the next line. When
you edit the text in your file, vi does not save your changes
There are many ways you can use the :s command,
until you give the command to do so.
including:


EDIT TEXT WITH VI




– The vi editor globally
¤ Type %s/ and the original ‹ Type a forward slash and
FIND AND REPLACE TEXT
word or phrase you want to the new word or phrase. replaces the old text with the
„ Type :. replace. replacement text.
› Type another forward
– The cursor moves to
slash, the letter g, and press
the bottom of the screen
Return.
following a colon.


72
5
WORK WITH TEXT EDITORS




You can copy text into the paste buffer without
You can cut-and-paste text in vi by using the
deleting it by using the y command, which stands
deletion keystroke commands and the p
for yank. As with deletion commands, you must
command. When you delete text, it is not
follow the y with a movement command that
completely lost; instead, vi stores it in a paste
indicates how many lines you want to yank, or you
buffer. You can think of this as a container that
can type yy to yank the current line.
holds the most recently deleted text.
To yank or delete a number of lines, you can
You can paste the contents of the paste buffer
precede the y or d keystrokes with a number. For
into the current location of the cursor by typing
example, to yank 12 lines, including the current
the letter p. For example, to move a line, delete it
one, type 12yy.
by typing dd and move to the new location in
the file. Type the letter p, and vi inserts the
deleted line into the new location.




– The vi editor joins the two
¤ Press Shift + J.
JOIN TWO LINES OF TEXT
lines together.
„ Position the cursor to the
first of two lines you want to
join.




73
UNIX FOR MAC




SAVE A FILE WITH VI
You may name your text file whatever you like, but for

W
hen you finish editing a file with the vi editor, you
compatibility with Aqua applications, you should append
can save your work. It is a good practice to use
text files with the file extension .txt. However, if you are
the save command any time you make changes
creating a specific type of text file, such as an HTML file, a
that you do not want to lose. To save the current file, you
Cascading Style Sheets file, or an XML settings file, you
can type :w and press Return. vi saves the file to your hard
must save it with the appropriate extension such as .html,
drive, and any changes you make become permanent. The
.css, or .plist.
vi editor displays the filename, the number of lines, and the
number of characters in the file after you save it.
To exit the vi editor, you can type the quit command as :q.
You can combine the :w and :q commands together as
You can save the file with a different name by typing a
:wq, which saves the file and exits the vi editor, returning
filename before pressing Return. For example, to save the
you to the shell. If you have made changes but do not want
current file as my-update.txt, you can type :w my-update.txt
to save them, you can exit vi by appending an exclamation
and press Return. If you start vi without specifying a
point (!) to the :q command.
filename, you must remember to save with a new name, or
else vi saves it in the /tmp directory with an arbitrary name
such as vi.XW0hyP.



SAVE A FILE WITH VI




¤ Type the letter w and ‹ Type a new filename and
SAVE A FILE WITH VI SAVE WITH A DIFFERENT
FILENAME
press Return. press Return.
„ Type :.
„ Type :.
– The vi editor saves the file. – The vi editor saves the file
– The cursor moves to
¤ Type the letter w and a under the new name.
the bottom of the screen
space.
following a colon.


74
5
WORK WITH TEXT EDITORS


One file you may want to edit is the .exrc file. This is a file
that you store in your Home directory and that vi editor
reads when it starts up. vi automatically runs the commands
in the file, so this makes it a good place to store your
preferred editor options, such as showmode. Because the
filename starts with a period, it is a hidden file, invisible to
the ls command, but you can still list it by typing ls -a.

EXRC File Example:
set autoindent
set ruler
set verbose
set showmode
˜
˜
˜

To use these settings, make sure you are in your Home
directory by typing cd, and then type vi .exrc. Enter the lines
above into the file, save it, and exit. The next time you start
vi, it automatically applies these options.




– The vi editor exits without
‹ Type wq and press Return.
SAVE AND EXIT VI EXIT VI WITHOUT SAVING
saving.
„ Open a file and add „ Type :.
– The vi editor saves the file
some text. and exits.
¤ Type q! and press Return.
¤ Type :.



75
UNIX FOR MAC




OPEN A FILE WITH EMACS
As with Pico, you can use the arrow keys to move around,

Y
ou can use the emacs editor to edit text files. emacs
and you can edit text directly.
is a very complex program that can do everything
from editing simple text files to downloading and
You can execute commands in emacs by using either the
displaying Web sites.
Control or Meta key. When you type a character with the
Control key, this corresponds to the prefix C- in emacs
Among advanced Unix users, there is a friendly rivalry
terminology; a character that you type with the Meta key
between users of vi and users of emacs. This book gives
corresponds to the prefix M-. For example, to search for
greater coverage to vi because it is less complex than
text, you press Control + S (C-s), and to move to the end of
emacs. However, if you do not find vi to your liking or you
a file, you press Esc + > (M->). Keep in mind that the prefix
prefer to use a very flexible and adaptable text editor, the
C- means that you must hold down the Control key while
emacs editor is an excellent choice.
pressing another key, and M- means you must press and
release the Esc key before typing the following key. You use
The basic functions of the emacs editor are easy to use. To
the Esc key instead of the Meta key because Apple
open a file, you can simply give the name of the file as an
keyboards do not have a Meta key.
argument, as with other text editors. Unlike vi, emacs does
not have different modes for Command and Input, so you
do not have to switch between modes as you would with vi.


OPEN A FIILE WITH EMACS




– The emacs editor displays – This area displays the
¤ Type the name of the file
OPEN A FILE WITH EMACS
and press Return. the file. document.
„ Type emacs and a space.
– This area displays the – This area displays the
filename. status line.




76
5
WORK WITH TEXT EDITORS




The following table is a summary of some of the most
useful emacs commands for moving and finding text.

KEYSTROKES COMMAND NAME EFFECT

<<

. 17
( 69 .)



>>