Inserting and editing in vi
While navigating the document, you remain in command mode and use the keyboard as an interface tool to move the cursor. The next set of command mode keys provide different ways to enter insert mode, which is used to input new text into your file. You used the most basic way to enter insert mode at the beginning of this tutorial when you entered your initial text: You pressed the i key, which stands for insert. Pressing i puts you into insert mode in front of the current cursor location.
For instance, if you navigate to the beginning of the file by pressing gg, you can
press i, which makes any text you enter appear prior to the
text on the current line. Press gg, i, and then type
I am inserting new text with i.
Press the Esc key when you're finished to go back to
command mode. Remember, you must go back into
command mode after entering new text, or there is no way
to navigate the document. See Figure 7.
Figure 7. The simplest way to add new next is to use the i key to enter insert mode
The other basic method of entering new text is to use the a key,
which stands for append. Using the a key puts you into
insert mode, but it adds text after the current location of
the cursor instead of before the current location of the
cursor. To test this, go to the last line of your document and press the G and $ key to go to the end of the
line. Then press the a key, type
Pressing a appends
text, and press the Esc key to return to
command mode. See Figure 8.
Figure 8. Another way to insert text is with the a key, which stands for append
Now your cursor is at the period on the last line of your file. If
you press the i key now, you'd insert text just before
the period. If you press the a key, you'd insert text just
after the period. By pressing the I key (the capital letter),
you can start your input at the beginning of the line, even
though your cursor is at the end of the line. Similarly, if you
press the capital A key, you can input
text at the end of the line regardless of the cursor position. To
test this, press I, type
I think , and then press the
Esc key. See Figure 9.
Figure 9. To insert text at the beginning of a line (regardless of cursor position), press the I key
Another useful way of inserting new text is to simultaneously enter insert mode and add a new line to your text file. Just like the normal text insertion, this can be done before or after the cursor location:
- To insert a new line above the current cursor location, press the O key.
- To insert a new line below the current cursor location, press the o key.
To try this command, press O, type
I inserted this line by
pressing O, and then press the Esc key to return to command
mode. See Figure 10.
Figure 10. To insert text on a new line before the cursor, press the O key
You've now used the major methods of inserting new text. To recap, the keys are a, i, A, I, o, and O. Can you remember what each one does? Don't worry, even if you don't, they will come natural to you after a few days of use.
Now that you've typed a few items into your tutorial.txt file,
you might find that you've made some errors, or you might want to
change your choice of words. Before learning how to delete
text, you should learn how to replace text. Why? Because if you
learn to add new text and delete old text, you might fall into the
habit of using the delete commands when a
would be more efficient. It's quicker to replace a word
in a one-step process than it is to delete a word and then
add a new word in its place.
vi uses two important replace commands. The first is the r key, which removes the character the cursor is focused on and puts you in insert mode for a single replacement character. In other words, you can make one keypress after pressing r. As a result, vi automatically returns to command mode (without pressing the Esc key). To try this, use the k key and the l key to navigate up to the end of second line. Your cursor should be focused on a period. To change the period to an exclamation point, press the r key and then press the ! key. See Figure 11.
Figure 11. To replace a single character, press the r key and then type the character you want in the document
An even more useful
replacement command is executed by
pressing the c key and then the w key, which together stand for
change word. This command deletes the current word
and puts you in insert mode so that you can immediately start typing
a replacement word. In this case, you need to press the Esc key
when you're finished typing the new word to let vi know you're
done. Move down to the word
great, press the c and w key, type
cool, and then press the Esc
key. See Figure 12.
Figure 12. To replace a word, press the c and w key and then type the new word
If you want to remove text altogether rather than replacing
it, you need to use a
delete command. As with many other
things in vi, you have multiple choices, depending on how much
data you want to delete at once. The most basic
is the x key, which deletes one character at a time. To try it, go
back to the beginning of your text file and press the Ctrl-u (page-up) key. Press the $ key to go to the
end of the line and then press the x key five times to delete the test. See Figure 13.
Figure 13. To delete individual characters, use the x key
Pressing the x key five times did the job, but you've probably gathered that vi has a quick way of doing just about everything. You can delete a word with one command by pressing the d key and w key, which together stand for delete word. Navigate back to the word This by pressing the b key three times. To delete the word, press the d key and w key. See Figure 14.
Figure 14. To delete individual words, use the dw command
You're left with a hanging, partial sentence at the end of the first line. To delete everything on a line that follows your cursor position, you can use the d key with the $ key, which you've learned is used for end-of-line operations. Press the d key and the $ key to remove is a from the end of the line. See Figure 15.
Figure 15. To delete from the cursor to the end of a line, use d$
The final commonly used deletion method in vi is the
which is accomplished by pressing the d key
twice. It deletes the text on the line and brings the
next lower line up, so you don't have an empty line in the document.
To remove the first line of your file, press the d key and then
press the key again. See Figure 16.
Figure 16. To delete an entire line, press dd.
Speaking of bringing up a line, you can use the
join command by
pressing the J key, which brings the line below the cursor up to
the line the cursor is on, without deleting any text. Your
cursor should be on the new line (line 1) of your document. Press the J
key to bring line 2 up to line 1. See Figure 17.
Figure 17. To bring two lines together in vi, press the J key