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
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
As another example, typing
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
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
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
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
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
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
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.