Lesson 3 | Searching for text in vi |
Objective | Use vi commands to search for text. |
Command | Action |
/pattern | Search forward for pattern |
?pattern | Search backward for pattern |
n | Repeat search in the same direction |
N | Repeat search in the opposite direction |
/
and ?
commands behave somewhat like ex
commands, because the /
and ?
appear on the status line. Also, you must press Enter after typing the pattern. After finding a pattern using the /
or ?
command, you can use n
or N
to search for the next occurrence.
The n
and N
commands are regular vi commands. They take effect immediately, with no need to press Enter.
.
, [ ]
, *,
^
and $
. They have the same meanings as when they are used with grep
commands. In vi, regular expressions do not need to be quoted, because vi provides its own environment separate from the shell.
However, you must still type a backslash before a character if you want to disable its meaning as regular expression syntax. For example, use \
. to search for an actual period.
Command | Action |
---|---|
h |
Move left |
l |
Move right |
j |
Move down |
k |
Move up |
w |
Move to the beginning of next word |
b |
Move to the beginning of previous word |
e |
Move to the end of current word |
0 |
Move to beginning of the line |
^ |
Move to first non-blank character of line |
$ |
Move to end of line |
G |
Go to the last line of the file |
gg |
Go to the first line of the file |
nG |
Go to line number n |
Command | Action |
---|---|
i |
Insert before cursor |
I |
Insert at beginning of line |
a |
Append after cursor |
A |
Append at end of line |
o |
Open a new line below current line |
O |
Open a new line above current line |
Command | Action |
---|---|
x |
Delete character under cursor |
dd |
Delete (cut) current line |
dw |
Delete word |
d$ |
Delete to end of line |
D |
Delete to end of line (same as d$ ) |
cw |
Change word |
C |
Change to end of line |
r |
Replace single character |
R |
Enter replace mode (overwrite text) |
Command | Action |
---|---|
yy |
Yank (copy) current line |
yw |
Yank word |
y$ |
Yank to end of line |
p |
Paste after cursor |
P |
Paste before cursor |
Command | Action |
---|---|
u |
Undo last change |
U |
Undo all changes to the current line |
Ctrl + r |
Redo undone change |
Command | Action |
---|---|
:w |
Save file |
:q |
Quit vi |
:wq or ZZ |
Save and quit |
:q! |
Quit without saving |
:x |
Save if modified and quit |
Command | Action |
---|---|
v |
Start visual mode (character-wise) |
V |
Visual line mode |
Ctrl + v |
Visual block mode |
Command | Action |
---|---|
. |
Repeat last change |
:set number |
Show line numbers |
:set nonumber |
Hide line numbers |
:syntax on |
Enable syntax highlighting |
:!command |
Execute external shell command |
apu 18 barney 02 bart 14 bernie 19 betty 04 cao 28 chris 40 chuck 13 daniel 41 dino 06 ellie 44 elroy 24 eric 31 frank 11 fred 01 /2.
/2.
indicates that the user is performing a forward search using the vi
editor./2.
will match any line where "2" is followed by any single character, such as 28
, 24
, or 20
.:ab
command to abbreviate text.