vi editing  «Prev  Next»
Lesson 3 Searching for text in vi
ObjectiveUse vi commands to search for text.

Searching for text in vi

In this lesson, you will learn how to search for text. The vi editor provides several commands for locating 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

The / 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.
The vi program understands regular expression[1] syntax. In vi, search patterns are actually regular expressions. This means you can use metacharacters such as ., [ ], *, ^ 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.

The `vi` editor has a wide range of commands. Below is a categorized list of additional common `vi` commands beyond the search ones you've already listed:
1. Movement Commands
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
2. Insertion Commands
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

3. Deletion and Change
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)


4. Copy & Paste (Yank & Put)
Command Action
yy Yank (copy) current line
yw Yank word
y$ Yank to end of line
p Paste after cursor
P Paste before cursor

5. Undo and Redo
Command Action
u Undo last change
U Undo all changes to the current line
Ctrl + r Redo undone change

6. Saving and Quitting
Command Action
:w Save file
:q Quit vi
:wq or ZZ Save and quit
:q! Quit without saving
:x Save if modified and quit

7. Visual Mode
Command Action
v Start visual mode (character-wise)
V Visual line mode
Ctrl + v Visual block mode

8. Miscellaneous
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

Design of Unix OS
The following series of images demonstrates text searches in vi: Searching for text using ?, /, n, and N
1) First open the phonelist file. The cursor appears in the upper left corner, at the start of line 1.
1) First open the phonelist file. The cursor appears in the upper left corner, at the start of line 1.

2) When you press/ to start a forward search, your cursor goes to the status line.
2) The image shows a Telnet session with a text-based interface. The visible content in the console window lists names with associated numbers, followed by a search command at the bottom.
Here is the output displayed in the console:
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.

Explanation:
  • The last line /2. indicates that the user is performing a forward search using the vi editor.
  • The regular expression /2. will match any line where "2" is followed by any single character, such as 28, 24, or 20.

When you press/ to start a forward search, your cursor goes to the status line.

3) Press Enter and the cursor moves to the text 28, which is the first instance that the regular expression 2 matches.
3) Press Enter and the cursor moves to the text 28, which is the first instance that the regular expression 2 matches.

4) To search again, press n, a command that does not appear on the screen.
4) To search again, press n, a command that does not appear on the screen.

5) Press? to a start a backward search. Notice the cursor again goes to the status line.
5) Press? to a start a backward search. Notice the cursor again goes to the status line.

6) Press Enter and the backward search begins from the last cursor position, 23 and goes up to 16, the nearest match.
6) Press Enter and the backward search begins from the last cursor position, 23 and goes up to 16, the nearest match.

In the next lesson, you will learn how to use the :ab command to abbreviate text.

Searching Text in vi- Exercise

Click the Exercise link below to practice finding text in vi.
Searching Text in vi - Exercise

[1]: regular expression: A regular expression describes a pattern using literal characters and optional metacharacters known as regular expression syntax.

SEMrush Software 3 SEMrush Banner 3