BELUG > vi > previous

Editing Text

vi offers a rich assortment of commands for editing text. Among the most basic are those used for deleting or erasing.

The x (lower case "x") command deletes the character immediately under (i,e., covered by) the cursor. To delete any desired character, just switch to the command mode (if you are not already there) and then use an appropriate combination of the h, j, k and l commands (of course, one at a time) to move the cursor to that character. Then type x and the character is deleted.

By pressing x continuously instead of just hitting it once, the cursor continuously moves to the right and each character under it is successively deleted.

The X (upper case "X") command is similar except that it deletes the character to the left of the cursor rather than the character under it.

There are several additional commands for deleting text. The D (upper case "D") command removes the text on the current line from the character under the cursor to the end of the line.

The d (lower case "d") command is very flexible because it can be modified to delete any number of characters, words or lines. Typing d by itself will not do anything, but typing dw causes the character the cursor is resting on and the remaining characters to the right of it in the same word to be deleted. (The "w" stands for "word.")

Typing 2dw causes the character under the cursor, the remaining characters to the right of it in the same word and all of the characters in the next word to be deleted. For example, typing 2dw with the cursor on the "a" of the string "pineapple plantation" causes the string "apple plantation" to be deleted.

As another example, typing 3dw with the cursor on the "j" of the string "the bluejay flew south" causes the string "jay flew south" to be deleted. That is, "jay" and two words to the right of it are deleted.

Deleting an entire line can be accomplished with the dd command. This command can also be used to delete multiple lines by preceding it with an integer representing the number of lines to be removed. For example, typing 2dd will delete two consecutive lines beginning with the current line.

With some terminals, deletion of a line causes it to be replaced on the screen with an "@" character. This character merely represents an empty line and is not inserted into the text. Its purpose is to relieve the processor from having to redraw the screen (i.e., change the whole screen). This character can be removed if desired by typing r (or l on some terminals) while holding down the CTRL key.

The change command c (lower case "c") differs from the delete command in that it not only deletes a section of text but also activates insert mode to allow you to type in replacement text. After you have completed typing in the replacement text, be sure to press ESC to return to the command mode.

As is the case with d, the c command is not used by itself but is only used in combination with another letter after it and an optional integer before it.

For example, the command cw (which stands for "change word") deletes the characters in the current word under and to the right of the cursor and then switches vi to the insert mode so that you can enter text to replace the deleted characters. The number of new characters typed in can be the same as, fewer or more than the number deleted.

The amount of text to be changed can be increased by preceding the command with a number. For instance, typing 2cw will additionally remove the next word for replacement with whatever is typed in. The space between the words is not preserved.

The d and c commands can also be modified by other characters in addition to "w." For example they can be used with "b," which stands for "back." Thus, typing 3bd will delete the characters to the left of the cursor in the current word together with the two words to the left of the current word.

The cc command erases the current line, leaving it blank and awaiting replacement text. Preceding this command with an integer will delete that number of lines, beginning with the current line. For example, typing 5cc will allow you to change five consecutive lines starting with the current line.

Another change command, R, differs from the c commands in that it does not initially delete anything. Rather, it activates insert mode and lets you replace the characters under the cursor one at a time with characters that you type in.

vi supports several types of transposition. Transposing the order of two adjacent characters is easy with the xp command. Just place the cursor on the left-most of the two characters, type x to erase the left character and then type p for the deleted character to be put to the right of the cursor.

Two adjacent words can be transposed with the deep command. To use it, position the cursor in the space just to the left of the word on the left and type deep. Two adjacent lines can be transposed with the ddp command by placing the cursor on the upper line and typing ddp.

It is also a simple matter to change the case of a letter. When the cursor is over the desired letter, hit the "~" (tilde) key. This will change a capital letter to a small letter and visa versa.

The J (upper case "J") command is used to join the next line to the current line. The opposite operation, splitting a line, is accomplished in insert mode by merely positioning the cursor over what will be the first character of the new line and then hitting ENTER.

vi also has an undo capability. The u (lower case "u") command is used to reverse the effects of an already issued command that has changed the buffer, but which is not yet written to disk. U (upper case "U") undoes all of the changes that have been made to the current line during your current visit to it.