Command Mode Editing
In this section we use a sample text file to perform some editing functions. The system must have been installed with the Kernel Development section if it's a Red Hat distribution, or the equivalent packages from a Debian distribution:
/usr/src/linux-2.4/Documentation/watchdog.txt
NOTE
The following is a suitable file for practicing editing commands:
/usr/src/linux-2.4/Documentation/watchdog.txt
Inserting and Adding Text
One of the most common tasks in a vi session is making changes to the text in a file. This involves using one of the following keys to change from Command mode to Insert mode:
iInserts text to the left of the cursor
IInserts text at the beginning of the text in that line, not the beginning column of the vi screen
aAppends to the right of the cursor
AAppends at the end of the current line
oBegins a new line below the current line
ODrops the current line and begins a new one in its place
Changing or Replacing Text
The following are incredibly useful when you're altering an existing file and need to change a character, a line, a sentence, or just a word:
cwChanges a single word from the current cursor position. To change a whole word, you put the cursor on the first character of it.
c$Changes the current line but doesn't extend to change the rest of a wrapped sentence on the screen.
rReplaces the character under the cursor.
RReplaces text on the same line until Esc is pressed, but it doesn't change text on the next line. Instead, it pushes it ahead of the current changes.
NOTE
Remember to use the force multipliers in the appropriate places. You can easily change multiple words by using 5cw or replace 10 characters with 10r.
Deleting Text and Lines
A more advanced use of vi is to remove or delete characters, words, or even lines. Be careful to check your deletions and changes, or press the u key to get things back to normal and try it again.
xDeletes a single character under the cursor
XDeletes a single character before the cursor
dwDeletes a single word that's currently under the cursor, from the cursor position onward
ddDeletes the current line entirely, regardless of the cursor position in the line
DDeletes all text from the current cursor position to the end of the line
dLDeletes all text from the cursor to the end of the screen
dGDeletes all text from the cursor to the EOF
d^Deletes all text from the beginning of the line to the cursor
NOTE
In the lexicon of computer software verbiage, delete in vi stores the cut or deleted string of text in the unnamed buffer for future use. This buffer's contents will be overwritten by any further cuts or deletions.
CAUTION
How you delete is important on the exam. If asked to delete from the current position to the end of the line, don't pick or type the keys that delete the whole linethere are no partial credits on the exam!