VI Tutorial - Lesson 7 - Text Insertion

Every editor provides a method to place text into a file and the VI editor offers you not one, but several convenient methods. For your sanity, it is suggested you read through this section, select one way of adding text, practice that method until you become proficient, and then begin to include the other methods as your skills increase. Attempting to learn to use all commands at once, while possible, leads to frustration and angry words.

The append, insert, and open commands all move the editor from command mode into text input mode. To use these commands efficiently, it is necessary to have a firm understanding of the meaning of these two modes. More information about modes is contained in the Organization of VI section.

As you recall, while in text input mode the terminal keyboard acts just like a typewriter and every key pressed enters a character into your file. The most common error experienced by users when using VI is forgetting which mode they are in. This forgetfulness often leads to trying to issue a command while in text input mode. This results in an odd combination of characters being placed into the text which frequently causes the user to direct negative words at the terminal followed by a little clean-up editing. If you are ever in doubt about which mode you are in, press the "ESC" key. If you are in command mode when you press "ESC", the terminal will "beep". If you are in text input mode, all will be silent. Of course, pressing the "ESC" key while in text input mode has now placed you in command mode, but at least you know where you are.

A user may decide to place text into a brand new file with either the insert or append command. The resulting action as seen by the user is the same with both commands. The open command can also be used on a new file, but differs from insert and append because open adds a blank line. Experiment with these commands to see their differences.

7.1 Append Command (a, A)

The append command means "to add after" and comes in two forms, the lower-case "a" and upper-case "A". This frequently used command allows you to place as much information into a new file or an existing file as you want until you exit text input mode by pressing the "ESC" key and return to command mode.

Pressing the "a" key will cause the cursor to move one space to the right of the current cursor position and await the new text. On most terminals, when you are appending text, the existing text seems to be overwritten. This is only temporary. As soon as you exit text input mode with the "ESC" key, the screen is redrawn and the material that was temporarily covered again appears.

The "A," form of append works in much the same way, except when the command is issued the cursor jumps to the end of the line and it is at this point that you enter text input mode and proceed to add text. Pressing "ESC" returns you to command mode and the screen is redrawn.

Below is an example of the lower-case "a" command appending new text after (to the right of) the cursor:

cursor before "a" command issued

Where is Purdue University?

as cursor appears after "a" command issued

Where is Purdue University?

as cursor appears after appending '23456'

Where i23456due University?

as line appears after "ESC"

Where i23456s Purdue University?

 

This is an example of the upper-case "A" command which appends after the last character in the line:

cursor before "A" command issued

Where is Purdue University?

cursor moves when "A" command issued

Where is Purdue University? 

as line appears after appending '23456'

Where is Purdue University?23456 

as line appears after "ESC"

Where is Purdue University?23456

 

7.2 Insert Command (i, I)

The insert command is used to insert text into the file being edited. The lower-case "i" command inserts the new text you type to the left of the cursor. The upper-case "I" command inserts the text at the beginning of the current line. By giving the insert command, text input mode is activated, allowing you to enter as much text as you want. To stop entering text you must press the "ESC" key. On most terminals while you are inserting new material, old text seems to be overwritten. All this text will reappear unchanged after the newly inserted text when you exit text input mode and reenter command mode by pressing the "ESC" key.

The example below demonstrates how the lower-case "i" command functions inserting new text before (to the left of) the cursor:

cursor before "i" command issued

Where is Purdue University?

as cursor appears after "i" command issued

Where is Purdue University?

as line appears after inserting '23456'

Where 23456rdue University?

as line appears after "ESC"

Where 23456is Purdue University?

 

This example illustrates the upper-case "I" command which inserts new text before (to the left of) the first character in the line:

cursor before "I" command issued

Where is Purdue University?

cursor moves when "I" command issued

Where is Purdue University?

as line appears after inserting '23456'

23456 is Purdue University?

as line appears after "ESC"

23456Where is Purdue University?

 

7.3 Open Command (o, O)

The open command is used to create a blank line in the file where additional text is to be typed in. The lower-case "o" command opens a new line below the line the cursor is on and the upper-case "O" command opens a new line above the line the cursor is on. By giving the open command, text input mode is activated and a blank line will appear on the screen, and the cursor will relocate to the first character space on this new line. You may now enter as much text as you want. To stop entering text you must press the "ESC" key.

The two examples below show the use of the open commands. The first example illustrates the lower-case "o" command opening a line below the current line and the second example illustrates the upper-case "O" command opening a line above the current line.

original text before "o" command issued

Where is Marian?
Where is Purdue?
Where is Mackey?

as cursor and text appear after "o" command issued

Where is Marian?
Where is Purdue?
 
Where is Mackey?

as cursor appears after keying '23456' and "ESC"

Where is Marian?
Where is Purdue?
23456
Where is Mackey?

original text before "O" command issued

Where is Marian?
Where is Purdue?
Where is Mackey?

as cursor and text appear after "O" command issued

Where is Marian?
 
Where is Purdue?
Where is Mackey?

as cursor appears after keying '23456' and "ESC"

Where is Marian?
Where is Purdue?
23456
Where is Mackey?

 

7.4 Read Command (:r)

The read command is another method of placing information into a file. It is quick, easy and useful. After you have mastered the control of the VI editor, come back and reread this section and practice this command. It is too handy not to make use of it.

This command allows the user to place a copy of another file into the current file. For example, you might keep a special file containing the heading required for all your lab assignments or a tricky, tough-to-type equation. In order to avoid retyping this information, all you do is tell the editor to "read" your special file into your current file, and like magic, this material from the special file becomes part of your current file with no pain and no typos.

While in command mode and with the cursor on the line above where you want the special file read in, type:

 

     :r filename

A copy of your special file will appear directly below the line the cursor was sitting on and the cursor will reposition to the first character of the first line in the newly added text. Here is an example of the read command:

current file opened with vi

aaaa aa aaaa aa aaaa.
bbbb bb bbbb bb bbbb.
cccc cc cccc cc cccc.
dddd dd dddd dd dddd.
-
-
-

"special" file to be added with ":r special" read command

1111111
2222 22222
3 333 33

current file after reading "special"

aaaa aa aaaa aa aaaa.
bbbb bb bbbb bb bbbb.
cccc cc cccc cc cccc.
1111111
2222 22222
3 333 33
dddd dd dddd dd dddd.

 


previous | MENU | next

 

Last modified: 2009/09/01 09:46:26.251000 GMT-4 by christopher.n.deckard.1
Created: 2007/11/06 14:24:30.993000 US/Eastern by brian.r.brinegar.1.

Categories

Search

Type in a few keywords describing what information you are looking for in the text box below.

Admin Options: Edit this Document