UNIX / Linux Tutorial

 
1.13.3 Inserting Text  |  1.13.4  Deleting Text  |  1.13.5 Changing  Text
1.13.6 Commands For Moving The Cursor  |  1.13.7 Saving Files And Quitting vi
1.13.8 Editing Another File
 
1.13.3 Inserting Text

The vi program is now in command mode. Insert text into the file by pressing i ,
which places the editor into insert mode, and begin typing:

Now is the time for all good men to come to the aid
of the party.
˜
˜
˜
˜
˜

Type as many lines as you want (pressing Enter after each). You may correct mistakes
with the Backspace key.

To end insert mode and return to command mode, press Esc .
In command mode you can use the arrow keys to move around in the file. (If you have
only one line of text, trying to use the up- or down-arrow keys will probably cause vi to
beep at you.)

There are several ways to insert text other than the i command. The a command inserts
text beginning after the current cursor position, instead of at the current cursor position. 
For example, use the left arrow key to move the cursor between the words "good" 
and "men.".

Now is the time for all good men to come to the aid
of the party.
˜
˜
˜
˜
˜

Press a to start insert mode, type "wo", and then press Esc to return to command mode:

Now is the time for all good women to come to the aid
of the party.
˜
˜
˜
˜
˜

To begin inserting text at the next line, use the o command. Press o and enter another
line or two:

Now is the time for all good humans to come to the
aid of the party.
Afterwards, we'll go out for pizza and beer.
˜
˜
˜
˜

1.13.4  Deleting Text

From command mode, the x command deletes the character under the cursor. If you
press x five times, you'll end up with:

Now is the time for all good humans to come to the
aid of the party.
Afterwards, we'll go out for pizza and
˜
˜
˜
˜

Now press a and insert some text, followed by esc :

Now is the time for all good humans to come to the
aid of the party.
Afterwards, we'll go out for pizza and Diet Coke.
˜
˜
˜
˜

You can delete entire lines using the command dd (that is, press d twice in a row). If
the cursor is on the second line and you type dd, you'll see:

Now is the time for all good humans to come to the
aid of the party.
˜
˜
˜
˜
˜

To delete the word that the cursor is on, use the dw command. Place the cursor on the
word "good", and type dw:

Now is the time for all humans to come to the aid of
the party.
˜
˜
˜
˜
˜

1.13.5 Changing Text

You can replace sections of text using the R command. Place the cursor on the first
letter in "party", press R , and type the word "hungry".

Now is the time for all humans to come to the aid of
the hungry.
˜
˜
˜
˜
˜

Using R to edit text is like the i and a commands, but R overwrites, rather than inserts,
text.

The r command replaces the single character under the cursor. For example, move the
cursor to the beginning of the word "Now", and press r followed by C, you'll see:

Cow is the time for all humans to come to the aid of
the hungry.
˜
˜
˜
˜
˜

The "˜" command changes the case of the letter under the cursor from upper- to lowercase,
and back. For example, if you place the cursor on the "o" in "Cow" above and
repeatedly press ˜ , you'll end up with:

COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF
THE HUNGRY.


˜
˜
˜
˜
˜

1.13.6 Commands For Moving The Cursor

You already know how to use the arrow keys to move around the document. In addition,
you can use the h, j, k and l commands to move the cursor left, down, up, and right,
respectively. This comes in handy when (for some reason) your arrow keys aren't working
correctly.

The w command moves the cursor to the beginning of the next word; the b command
moves it to the beginning of the previous word.

The 0 command (that's the zero key) moves the cursor to the beginning of the current
line, and the $ command moves it to the end of the line.

When editing large files, you'll want to move forwards or backwards through the file
a screen at a time. Pressing Ctrl-F moves the cursor one screen forward, and
Ctrl-B moves it a screen back.

To move the cursor to the end of the file, press G. You can also move to an arbitrary
line; for example, typing the command 10G would move the cursor to line 10 in the file.
To move to the beginning of the file, use 1G.

You can couple moving commands with other commands, such as those for deleting
text. For example, the d$ command deletes everything from the cursor to the end of the
line; dG deletes everything from the cursor to the end of the file, and so on.

1.13.7 Saving Files And Quitting vi

To quit vi without making changes to the file, use the command :q!. When you press
the ":", the cursor moves to the last line on the screen and you'll be in last line mode:

COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF
THE HUNGRY.
˜
˜
˜
˜
˜
: _        


In last line mode, certain extended commands are available. One of them is q!, which 
quits vi without saving. The command :wq saves the file and then exits vi. The command
 ZZ (from command mode, without the ":") is equivalent to :wq. If the file has not been
changed since the last save, it merely exits, preserving the modification time of the last
change. Remember that you must press Enter after a command entered in last line
mode.

To save the file without quitting vi, use : w.

1.13.8 Editing Another File

To edit another file, use the :e command. For example, to stop editing test and edit
the file foo instead, use the command:

COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF
THE HUNGRY.
˜
˜
˜
˜
˜
:e foo_     


If you use :e without saving the file first, you'll get the error message:

            No   write   since   last   change   (": edit! "    overrides )

which means that vi doesn't want to edit another file until you save the first one. At this
point, you can use :w to save the original file, and then use :e, or you can use the 
command:

COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF
THE HUNGRY.
˜
˜
˜
˜
˜
:e!       foo_       


The "!" tells vi that you really mean it—edit the new file without saving changes to the
first.

HOME

1.1 Introduction   1.2.10 Referring To Home Directories   1.3.4  Copying Files

1.6 Exploring The File System   1.8   Wildcards   1.9.3 Pipes   1.10.3 Permissions Dependencies

1.12.4  Stopping And Restarting Jobs   1.13.3 Inserting Text   1.13.9 Including Other Files

1.14.3 Shell Initialization Scripts   System Administration   2.3.1 The /etc/imitate file

2.4 Managing File Systems   2.6 Managing Users  2.6.5 Groups   2.7.2 gzip and compress

2.8.3 Making Backups To Tape Devices   2.9.1 Upgrading The Kernel   

2.9.3 Installing A Device Driver Module

BOOK: LINUX QUICK COMMAND REFERENCE

http://personal.atl.bellsouth.net/~psadler

© copyright KnowledgeWorks, Inc. (2001)