The vi editor provides commands for deleting or
replacing single characters, single or multiple words, and single or multiple
lines of text. Table 9.4 shows some common delete and replace commands with
their associated actions. Remember you need to be in command mode to use these.
Command |
Action |
x |
Delete one character of text |
r |
Replace one character of text with
the next character entered |
dw |
Delete entire word (3dw deletes 3
words) |
dd |
Delete entire line (3dd deletes 3
lines) |
D |
Delete from cursor to end of line |
s |
Switch to insert mode after deleting
current character |
cw |
Delete entire word and switch to
insert mode |
cc |
Delete entire line and switch to
insert mode |
C |
Change (delete and switch to insert
mode) from cursor position to end of line |
Table 9.4: Delete or replace text commands
Searching for Text Strings
The vi editor allows the user to search for a text
string either forward (down) in the file or backward (up). It also allows a
shortcut for repeating the search. A special search capability that comes in
handy when writing shell scripts is the ability to search for matching
parentheses, brackets, and braces. Table 9.5 below shows some search commands
and their associated actions.
Command |
Action |
/text |
Search forward in the file for text |
?text |
Search backward (up) in the file for
text |
% |
With the cursor on a parentheses,
bracket, or brace character, you can press the % key to move the cursor to
its matching open or close character |
Table 9.5: Search commands and their associated actions
Cutting, Copying, and Pasting Text
Any of the text deleting commands presented earlier work
similarly to the cut feature of Windows in that they place the deleted text in a
buffer area for potential retrieval at a later time. In Windows, the buffer is
referred to as the clipboard.
In vi there is a unnamed default buffer and 26
specifically referenced buffers, each identified by one of the letters of the
alphabet (a, b, c, d, etc.). In order to reference one of the named buffers,
the buffer identifier character is preceded with a single open quotation. So,
?a refers to buffer a, ?b refers to buffer b, and so on.
When one of the delete commands is used, the text is cut
from the document and placed in the default buffer. To retrieve text from the
default buffer, the upper case P command can be used to paste the default buffer
before the current cursor line, or the lower case p command can be used to paste
the contents of the buffer after the current line. Therefore, a 5dd command
followed by a cursor movement and a P command would cut five lines of text and
place them before the line where the cursor was moved.
Instead of cutting or deleting text, it is also possible
to copy text from a file using the yank (yy) command. The yank command presents
the option of copying text to one of the specific named buffers. Where yy would
copy the current line to the unnamed default (unnamed), ?cyy would copy the
current line to the buffer named c.
The issuance of multiple yank commands to the same
buffer without intervening paste commands will result in buffer overwrites. In
other words, the user cannot yank line five to buffer a, then yank line seven to
buffer a and expect to be able to paste both lines five and seven somewhere.
When a user yanks line five, it is placed in buffer a as requested, but when a
command to yank line 7 to buffer a follows, line 7 will overwrite line five,
which is sitting in the buffer. This is one of the reasons for providing
multiple named buffers to use for multiple successive yanks. Table 9.6 shows
copy and paste commands and their associated actions.
Command |
Action |
yy |
Copy (yank) the current line of text
into the default (unnamed) buffer |
?byy |
Copy (yank) the current line of text
into the buffer named b |
5yy |
Copy five lines of text to the
default buffer |
p |
Paste the default buffer after the
current cursor line |
P |
Paste the default buffer before the
current cursor line |
?bP |
Paste the contents of named buffer b
before the current cursor line |
Table 9.6: Copy and paste commands and their actions
Undo and Other Useful Commands
Table 9.7 shows some additional miscellaneous commands
and their associate actions. Most important may be the u command which will
undo the last change that was made. In most vi editors you can undo several of
the most recent commands.
Command |
Action |
J |
Join the current cursor line with the
next line in the file |
Enter |
Split the current line at the cursor
position when in insert mode. |
u |
Undo the last change that was made |
U |
Undo any changes made to the current
cursor line |
:r filename |
Read the file named filename and
insert it below the current cursor line |
Table 9.7: Miscellaneous commands and their associated
actions
vi Reference
For your convenience we have compiled the tables of vi
commands together for quick reference.
Command |
Action |
a |
Append text to the right of the
cursor |
i |
Insert text to the left of the cursor |
o |
Insert a new line below the current
line |
A |
Append text to the end of the current
line |
I |
Insert text at the beginning of the
current line |
O |
Insert a new line above the current
line |
Insert Commands
To return to command mode from insert mode use the
escape key.
Command |
Action |
:w |
Write the file to disk |
:wq |
Write the file to disk and quit the
editor |
<shift>ZZ |
Same as :wq |
:w! newfile |
Write the file to a new disk file
called newfile |
Write Commands
Command |
Action |
h |
Move cursor one position to the left
(left arrow) |
j |
Move cursor one line down (down
arrow) |
k |
Move cursor one line up (up arrow) |
l |
Move cursor one position to the right
(right arrow) |
^ |
Move to the beginning of the current
line |
$ |
Move cursor to the end of the current
line |
b |
Move to beginning of previous word |
w |
Move to beginning of next word |
e |
Move to end of next word |
G |
Move to end of the file |
:n |
Move to line n |
Enter |
Move to the first word one the next
line |
ctrl+b |
Page backward (up) |
ctrl+f |
Page forward (down) |
Cursor Movement
Command |
Action |
x |
Delete one character of text |
r |
Replace one character of text with
the next character entered |
dw |
Delete entire word (3dw deletes 3
words) |
dd |
Delete entire line (3dd deletes 3
lines) |
D |
Delete from cursor to end of line |
s |
Switch to insert mode after deleting
current character |
cw |
Delete entire word and switch to
insert mode |
cc |
Delete entire line and switch to
insert mode |
C |
Change (delete and switch to insert
mode) from cursor position to end of line |
Delete and Replace Commands
Command |
Action |
/text |
Search forward in the file for text |
?text |
Search backward (up) in the file for
text |
% |
With the cursor on a parentheses,
bracket, or brace character, you can press the % key to move the cursor to
its matching open or close character |
Search Commands
Command |
Action |
yy |
Copy (yank) the current line of text
into the default buffer |
?byy |
Copy (yank) the current line of text
into the buffer named b |
5yy |
Copy five lines of text to the
default buffer |
p |
Paste the default buffer after the
current cursor line |
P |
Paste the default buffer before the
current cursor line |
?bP |
Paste the contents of named buffer b
before the current cursor line |
Copy and Paste Commands
Command |
Action |
J |
Join the current cursor line with the
next line in the file |
Enter |
Split the current line at the cursor
position when in insert mode. |
u |
Undo the last change that was made |
U |
Undo any changes made to the current
cursor line |
:r filename |
Read the file named filename and
insert it below the current cursor line |