-->
Previous Table of Contents Next


Moving the Cursor

Moving the cursor around in vi essentially involves the following four keys:

h Moves the cursor one space to the left
j Moves the cursor down one line
k Moves the cursor up one line
l Moves the cursor one space to the right

These keys can perform their operations only when vi is in command mode. For convenience, most implementations of vi map these keys to their directional counterparts on the keyboard arrow keys.

vi enables you to move through a file in bigger “leaps” as well. Below are some commands for scrolling more than one line at a time:

Ctrl+u Scrolls up a half-screen
Ctrl+d Scrolls down a half-screen
Ctrl+f Scrolls down one full screen
Ctrl+b Scrolls up one full screen

The size of these movements largely depends on the terminal settings.

It is also possible to move the cursor to a specific line in a file. If you want to move to the fifth line, type 10G or :10 while in command mode. G by itself moves the cursor to the end of the file. The cursor does not move if the number given is not applicable (for example, typing :10 in an eight-line file has no effect).

vi also enables you to move the cursor one word at a time. A word is defined as any sequence of non-whitespace characters. To move to the beginning of the next word on the current line, press w. Press b to move the cursor to the beginning of the current or previous word.

Deleting Text

vi has commands for deleting characters, lines, and words. Deletion means that the selected text is removed from the screen but is copied into an unnamed text buffer from which it can be retrieved.

To delete a word, use the dw command. If you want to delete the word to the right of the cursor, type dw. You can also delete several words at a time. For example, the command 4dw deletes the next four words on the current line.

Lines can be deleted individually or by specifying a range of lines to delete. To delete the current line, enter dd. The command 4dd deletes four lines (the current line and three below it). dG deletes all lines from the current one to the end of the file.

On the current line, you can delete in either direction: d^ deletes backward to the beginning of the line; d$ (or D) deletes forward to the end of the line.

To delete individual characters, x deletes the character underneath the cursor, and X deletes the character to the left of the cursor. Both of these commands accept a number modifier: For example, 4x deletes the current character and the four characters to the right.

Unwanted changes such as deletions can be immediately undone by the u command. This “rolls back” the last edit made.

Copying and Moving Text

Moving sections of text around in a file basically requires three steps:

1.  “Yank” the text into a buffer.
2.  Move the cursor to where you want to insert the text.
3.  Place the text from the buffer at the new location.

Yanking text means to copy it into either a named or unnamed buffer. The unnamed buffer is a temporary storage space in memory that is continually overwritten by successive yanks. vi has 26 named buffers that correspond to each letter of the alphabet.

To yank the current line into the unnamed buffer, the command is yy or Y. These commands can be modified by a number indicating how many lines beneath the cursor are to be yanked. For example, the command


3yy

in your file asong (with the cursor on the top line) yanks the following text into the temporary buffer:


Down I walk

by the bay,

Where I can

This text can also be yanked into the named buffer a by the following command:


“a3yy

The double quote () tells the yank command to overwrite the contents of the named buffer a. If you type a capital A instead of a lowercase a, the three lines are appended to the end of the a buffer. This overwrite-versus-append concept works the same for all the named buffers.

If you move the cursor to the end of the file using the :$ command, you can then paste the contents of the unnamed buffer to the end of the file. This is done using the p command, which pastes the contents of a buffer to the right of the cursor (P pastes to the left of the cursor). The paste command can also specify a named buffer in the same way as the yank command:


“ap

Yanks can also be performed on words using the command yw. This command can also use named buffers and accepts numeric modifiers.


Previous Table of Contents Next