Manipulate Text in the Vi Text Editor for UNIX


This document also uses several fonts to help clarify the meaning of the text.

fixedLiteral commands you type appear in Courier font
 italicVariables used with commands will appear in italics
 boldKeys or elements that you must click appear in bold
 [ ]Optional variables will appear in square brackets

 
Copying ("Yanking") and Pasting Text

Vi allows you to copy or delete blocks of text and place them elsewhere in your document. The put commands below will insert into the document any text which has been put into the buffer by the delete commands above or the yank (copy) commands below:

[num] ywcopy num words into the buffer, starting with the current word from the cursor on
[num] yycopy num lines into the buffer, starting with the current line
[num] Ycopy num lines into the buffer, starting with the current line
pput any text in the buffer after or below the cursor
Pput any text in the buffer before or above the cursor

 
The example below illustrates the copying feature. In this example, the cursor is initially positioned before the word uncomment, next type 4yy to yank 4 lines into the buffer, then move the cursor to a destination position where you will place the text. Type p, to place the contents in the buffer below the cursor.

Copying text Example

Note: The four lines have been duplicated immediately below their original position.

Deleting Text

The following commands allow you to delete single characters, words, or whole lines of text with a single command. Most can be preceded by a number to delete more than one character, word, or line at a time. For example, 3dd would delete three lines at once. When the number is not specified, one character, word, or line is assumed. These commands put the deleted text into the buffer, which can then be placed elsewhere in the document by using the put commands explained later in this guide. While in input mode, you can use the Backspace key to correct mistakes on the current line of text.

xdelete character under cursor
[num] xdelete num characters from cursor forward
Xdelete character before the cursor
[num] Xdelete num characters before the cursor
[num] dwdelete num words starting with the current word from the cursor on
Ddelete text from the cursor to the end of the current line
[num] dddelete num lines starting with the current line


Changing Text

Vi has several commands that allow you to change an existing piece of text without first deleting it; the change commands overtype the current text.

[num] cwchange num words, starting at the cursor in the current word - the word(s) will be replaced by any text you type until you press Esc
Cchange the current line from cursor to end - the current line will be replaced by any text you type until you press Esc
rreplace the current character (you do not need to press Esc)
Redit the current line from cursor to end in typeover mode - you will remain in typeover mode until you press Esc (you can press Return to insert more lines before pressing Esc) 
sreplace the current character - the current character will be replaced by any text you type until you press Esc
Sreplace the entire current line - the current line will be replaced by any text you type until you press Esc