Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
6.  Using the vi Editor Basic vi Commands Changing Text Changing a Word  Previous   Contents   Next 
   
 

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.

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.

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 (you do not need to press Esc).

Undoing Changes

When you edit text and make changes to a vi file, you might occasionally wish that you had not changed something. vi's undo commands enable you to back up one operation and continue on from there.

Undoing the Previous Command

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.

Undoing Changes to a Line

Type U to undo all changes you've made to a line. This command works only if you haven't moved the cursor off the line. You do not need to press Esc after you type U.

Deleting Text

These vi commands delete the character, word, or line you indicate. vi stays in command mode, so any subsequent text insertions must be preceded by additional commands to enter entry mode.

Deleting One Character

To delete one character, position the cursor over the character to be deleted and type x.

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 one character before (to the left of) the cursor, type X (uppercase).

Deleting a Word or Part of a Word

To delete a word, position the cursor at the beginning of the word and type dw. The word and the space it occupied are removed.

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.

Deleting a Line

To delete a line, position the cursor anywhere on the line and type dd. The line and the space it occupied are removed.

Copying and Moving Text -- Yank, Delete, and Put

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.

Copying Lines

Copying a line requires two commands: yy or Y ("yank") and either p ("put below") or P ("put above"). Note that Y does the same thing as yy.

To yank one line, follow these steps.

  1. Position the cursor anywhere on the line you want to yank.

  2. Type yy.

  3. Move the cursor to the line above where you want to put (copy) the yanked line.

  4. Type p.

    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.

You can also use the P or p commands immediately after any of the deletion commands discussed earlier. This action puts the text you deleted above or below the cursor, respectively.


Caution - Use only cursor-moving commands between yanking or deleting and putting. If you delete or yank any other text before putting the new text in place, the lines you yanked or deleted are lost.


Moving Lines

Moving lines also requires two commands: dd ("delete") and either p or P.

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.

Using a Count to Repeat Commands

Many vi commands can be preceded by a repeat factor (called a count)--a number that precedes the command and tells it how many times to repeat the operation.

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.

Typing a period (.) repeats the previous text-changing command. For example, if you have just deleted a line with dd, you can move the cursor to another line and delete it by simply typing a period.

Using ex Commands

ex commands are more accurate and convenient than yank, delete, and put when you're dealing with large blocks of text. Rather than counting lines on the screen and then searching for an insertion point, you give vi a range of lines to be moved or copied and then specify the line before the insertion point. Of course, with a delete command there is no insertion point.

Turning Line Numbers On and Off

To turn line numbers on, type :set nu and press Return.

Line numbers appear in the left margin. Note that these numbers do not appear when you print out the file. They are visible only on the screen.

1 Oh, when I die, take my saddle from the wall,
2 Put it on my pony, lead him out of the stall.
3 Tie my bones to his back, point our faces to the west,
4 And we'll ride the prairies that we love the best.
5
6 Ride around, little doggies,
7 Ride around real slow.
8 Firey and Snuffy are rarin' to go.
~
~
~
~
~
~
:set nu

To turn line numbers off, type :set nonu and press Return.

 
 
 
  Previous   Contents   Next