OpenWindows Advanced User's Guide

6.5.3 Changing Text

Changing text involves substituting one section of text for another. vi has several ways to do this, depending on circumstances.

6.5.3.1 Changing a Word

To replace a word, position the cursor at the beginning of the word to be replaced. Type cw, followed by the new word. To finish, press Esc.

To change part of a word, place the cursor on the word, to the right of the portion to be saved. Type cw, enter the correction, and press Esc.

6.5.3.2 Changing a Line

To replace a line, position the cursor anywhere on the line and type cc. The line disappears, leaving a blank line for your new text (which can be of any length). Press Esc to finish.

6.5.3.3 Changing Part of a Line

To replace part of a line, place the cursor to the right of the portion to be saved. Type C, enter the correction, and press Esc. This changes the portion of the line from the current cursor position to the end of the line.

6.5.3.4 Substituting Character(s)

To substitute one or more characters for the character under the cursor, type s, followed by the new text. Press Esc to return to command mode.

6.5.3.5 Replacing One Character

Use this command to replace the character highlighted by the cursor with another character. Position the cursor over the character and type r, followed by just one replacement character. After the substitution, vi automatically returns to command mode (there's no need to press Esc).

6.5.3.6 Transposing Characters

Correcting transposed characters takes just two keystrokes in vi. Suppose you find that you've typed "teh" when you meant to enter "the". Make the correction by putting the cursor over the first letter to be moved (in this case, e), and then type xp. The e and h will trade places - and vi will automatically return to command mode.

6.5.3.7 Breaking or Joining Lines

To break a line without affecting text, move the cursor to a space where you want the line to break and type r (for "replace") followed by Return. Note that if you type r with the cursor on a character and then press Return, that character will be replaced by the Return.

To join two lines, place the cursor on the upper line and type an uppercase J. (There's no need to press Esc after typing J.)