The following sections explain the following categories of vi commands.
Moving around in a file
Changing and substituting text
Undoing changes to text
Checking your spelling
Formatting your file output
In the previous sections you learned how to create, save, print, and exit a vi file. Now that you have created a file, you'll need to understand the concepts that are required to navigate within it. Open your practice file now, and try each of the commands that are discussed in this section.
When you start vi, the cursor is in the upper left corner of the vi screen. In command mode, you can move the cursor with a number of keyboard commands. Certain letter keys, the arrow keys, and the Return key, Back Space (or Delete) key, and the Space Bar can all be used to move the cursor when you're in command mode.
If your machine is equipped with arrow keys, try these now. You should be able to move the cursor freely about the screen by using combinations of the up, down, right, and left arrow keys. Notice that you can only move the cursor across already existing text or input spaces.
If you're using vi from a remote terminal, the arrow keys might not work correctly. The arrow key behavior depends on your terminal emulator. If the arrow keys don't work for you, you can use the following substitutes:
If you move down when the cursor is at the bottom of the screen, or move up when the cursor is at the top of the screen, you will see the text scroll up or down. This scrolling can be an effective way to display more text in a short file, but it can be tedious to move this way through a long file.
You can page or scroll backward or forward through a file, a screen or a half-screen at a time. To try out these commands on paint, you might want to add text so you have a longer file to work with.
Note that paging and scrolling are fundamentally different. Scrolling actually scrolls the cursor up or down through the text a line at a time, as though it were on a paper scroll. Paging moves the cursor up or down through the text a screenful at a time. On a fast system, you might not notice the difference. However, if you're working from a remote terminal or in any other situation where your system is running slower than usual, this difference can become painfully apparent.
vi provides many commands for inserting text. This section introduces you to the most useful of these commands. Note that each of these commands places vi in entry mode. To use any of these commands, you must first be in command mode. Remember to press Esc to make sure you are in command mode.
Type A to add text to the end of a line. To see how this command works, position the cursor anywhere on a text line and type A. The cursor moves to the end of the line, where you can type your additions. Press Esc when you are finished.
To change part of a word, place the cursor on the word, to the right of the portion to be saved. Type cw, type the correction, and press Esc.
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 (you do not need to press Esc).
If you make a mistake in vi or if you just change your mind after an operation is completed, you can undo your last command by pressing u immediately after the command. You do not need to press Esc after you type u. By pressing u a second time you undo the undo.
The x command also deletes the space the character occupied—when a letter is removed from the middle of a word, the remaining letters will close up, leaving no gap. You can also delete blank spaces in a line with the x command.
To delete part of a word, position the cursor on the word to the right of the part to be saved. Type dw to delete the rest of the word.
Many word processors allow you to “copy and paste” and “cut and paste” lines of text. The vi editor also includes these features. The vi command-mode equivalent of “copy and paste” is yank and put. The equivalent of “cut and paste” is delete and put.
The methods for copying or moving small blocks of text in vi involves the use of a combination of the yank, delete, and put commands.
To yank one line, follow these steps.
Position the cursor anywhere on the line you want to yank.
Move the cursor to the line above where you want to put (copy) the yanked line.
A copy of the yanked line appears in a new line below the cursor.
To place the yanked line in a new line above the cursor, type P.
The yy command works well with a count: to yank 11 lines, for example, type 11yy. Eleven lines, counting down from the cursor, are yanked, and vi indicates this with a message at the bottom of the screen: 11 lines yanked.
To move one line, position the cursor anywhere on the line and type dd. For example, to delete 5 lines, type 5dd.
Next, move the cursor to the line above where you want the deleted line reinserted and type p. This inserts the text on a new line below the cursor.
Alternatively, you can put the deleted line above the cursor by typing P.
Most of the commands in the previous sections take counts. For instance, 3dd repeats the command to delete a line three times, therefore deleting three lines. 2dw deletes two words, and 4x deletes four characters or spaces. You can also use counts with commands to move the cursor, such as 3w and 2Ctrl-F. In the section Summary of Basic vi Commands each command that takes a count is indicated by “[count]” before the command name.