Command Mode: Deleting and Changing Text


This section describes the commands to delete and replace, or change, text in the document you are editing. The Undo command is covered here because it allows you to restore deleted or changed text.

Undoing Changes

Undo (u/U)

The u command (Undo) restores text that you just deleted or changed by mistake. A single Undo command restores only the most recently deleted text. If you delete a line and then change a word, the first Undo restores only the changed word; you have to give a second Undo command to restore the deleted line. The U command restores the last line you changed to the way it was before you started changing it, even after several changes.

Deleting Characters

Delete character (x/X)

The x command deletes the current character. You can precede the x command by a Repeat Factor (page 191) to delete several characters on the current line, starting with the current character. The X command deletes characters to the left of the cursor.

Deleting Text

Delete (d/D)

The d (Delete) command removes text from the Work buffer. The amount of text that d removes depends on the Repeat Factor and the Unit of Measure (page 188) you enter after the d. After the text is deleted, vim is still in Command mode.

Tip: Use dd to delete a single line

The command d RETURN deletes two lines: the current line and the following one. Use dd to delete just the current line, or precede dd by a Repeat Factor (page 191) to delete several lines.


You can delete from the current cursor position up to a specific character on the same line. To delete up to the next semicolon (;), give the command dt; (see page 169 for more information on the t command). To delete the remainder of the current line, use D or d$. Table 6-1 lists some Delete commands. Each command, except the last group that starts with dd, deletes from/to the current character.

Table 6-1. Delete command examples

Command

Result

dl

Deletes current character (same as the x command)

d0

Deletes from beginning of line

d^

Deletes from first character of the line (not including leading SPACEs or TABs)

dw

Deletes to end of word

d3w

Deletes to end of third word

db

Deletes from beginning of word

dW

Deletes to end of blank-delimited word

dB

Deletes from beginning of blank-delimited word

d7B

Deletes from seventh previous beginning of blank-delimited word

d)

Deletes to end of sentence

d4)

Deletes to end of fourth sentence

d(

Deletes from beginning of sentence

d}

Deletes to end of paragraph

d{

Deletes from beginning of paragraph

d7{

Deletes from seventh paragraph preceding beginning of paragraph

d/text

Deletes up to the next occurrence of word text

dfc

Deletes on current line up to and including next occurrence of character c

dtc

Deletes on current line up to next occurrence of c

D

Deletes to end of line

d$

Deletes to end of line

dd

Deletes the current line

5dd

Deletes five lines starting with the current line

dL

Deletes through last line on screen

dH

Deletes from first line on screen

dG

Deletes through end of Work buffer

d1G

Deletes from beginning of Work buffer


Tip: Exchange characters and lines

If two characters are out of order, position the cursor on the first character and give the commands xp. If two lines are out of order, position the cursor on the first line and give the commands ddp. See page 176 for more information on the Put commands.


Changing Text

Change (c/C)

The c (Change) command replaces existing text with new text. The new text does not have to occupy the same space as the existing text. You can change a word to several words, a line to several lines, or a paragraph to a single character. The C command replaces the text from the cursor position to the end of the line.

The c command deletes the amount of text specified by the Repeat Factor and the Unit of Measure (page 188) and puts vim in Input mode. When you finish entering the new text and press ESCAPE, the old word, line, sentence, or paragraph is changed to the new one. Pressing ESCAPE without entering new text deletes the specified text (replaces the specified text with nothing).

Table 6-2 lists some Change commands. Except for the last two, each command changes text from/to the current character.

Table 6-2. Change command examples

Command

Result

cl

Changes current character

cw

Changes to end of word

c3w

Changes to end of third word

cb

Changes from beginning of word

cW

Changes to end of blank-delimited word

cB

Changes from beginning of blank-delimited word

c7B

Changes from beginning of seventh previous blank-delimited word

c$

Changes to end of line

c0

Changes from beginning of line

c)

Changes to end of sentence

c4)

Changes to end of fourth sentence

c(

Changes from beginning of sentence

c}

Changes to end of paragraph

c{

Changes from beginning of paragraph

c7{

Changes from beginning of seventh preceding paragraph

ctc

Changes on current line up to next occurrence of c

C

Changes to end of line

cc

Changes the current line

5cc

Changes five lines starting with the current line


Tip: dw works differently from cw

The dw command deletes all the characters through (including) the SPACE at the end of a word. The cw command changes only the characters in the word, leaving the trailing SPACE intact.


Replacing Text

Substitute (s/S)

The s and S (Substitute) commands also replace existing text with new text (Table 6-3). The s command deletes the character the cursor is on and puts vim into Input mode. It has the effect of replacing the single character that the cursor is on with whatever you type until you press ESCAPE. The S command does the same thing as the cc command: It changes the current line. The s command replaces characters only on the current line. If you specify a Repeat Factor before an s command and this action would replace more characters than exist on the current line, s changes characters only to the end of the line (same as C).

Table 6-3. Substitute command examples

Command

Result

s

Substitutes one or more characters for current character

S

Substitutes one or more characters for current line

5s

Substitutes one or more characters for five characters, starting with current character


Changing Case

The tilde (~) character changes the case of the character under the cursor from uppercase to lowercase, or vice versa. You can precede the tilde with a number to specify the number of characters you want the command to affect. For example, 5~ will transpose the next five characters starting with the character under the cursor, but it will not transpose characters past the end of the line the cursor is on.




A Practical Guide to UNIX[r] for Mac OS[r] X Users
A Practical Guide to UNIX for Mac OS X Users
ISBN: 0131863339
EAN: 2147483647
Year: 2005
Pages: 234

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net