Javatpoint Logo
Javatpoint Logo

vi Searching

In command mode, with the help of '/' , string can be searched in forward direction and with the help of ? , string can be searched in backward direction. For example, /abc will do a forward search for string abc whereas ?abc will do a backward search for string abc.

/string

Here string has to be replaced by the searching word.

The /string allows us to do forward search for a string. If the same string is placed before and after the cursor, it will only indicate the string after the cursor. When you'll start typing /string in command mode, at lower left corner, typed word will be seen.

Example:

/readable

Linux Vi Searching1

Look at the above snapshot, this is our normal script. We have to search word readable from this script.

Linux Vi Searching2

Look at the above snapshot, after typing /readable and pressing enter, mentioned word on its first occurence will be automatically highlighted. Please note here, first occurence is in forward search. The string readable is also present before the cursor, but it searches forward.

?string

The ?string allows us to do backward search for a string. If the same string is placed before and after the cursor, it will only indicate the string before the cursor. When you'll start typing ?string in command mode, at lower left corner, typed word will be seen.

Example:

/readable

Linux Vi Searching3

Look at the above snapshot, this is our normal script. We have to search word readable from this script.

Linux Vi Searching4

Look at the above snapshot, after typing ?readable and pressing enter, mentioned word on its first occurence will be automatically highlighted. Please note here, first occurence is in backward search. The string readable is also present after the cursor, but it searches backward.

/^string

This command searches a string in forward direction but at the beginning of a line. If no such word is present at the beginning of the line, it will show a no match message.

Example:

/^readable

Linux Vi Searching5

Look at the above snapshot, after typing /^readable, cursor moved to the string which is at the starting of the line.

/string$

This command searches string in forward direction but at the end of a line. If no such word is present at the end of the line, it will show a no match message.

Example:

/readable$

Linux Vi Searching6

Look at the above snapshot, after typing /readable$, cursor moved to the string which is at the end of the line.

n

The n command indicates the next occurence of the searched string.

Linux Vi Searching7

Look at the above snapshot, cursor is at first occurence of string.

Linux Vi Searching8

Look at the above snapshot, after pressing n, cursor is at next occurence of the string.

/\

This command only searches for specifically the word enclosed in <>.

Example 1:

/\

Linux Vi Searching9

Look at the above snapshot, after typing /\<he\> command, we got the word he.

Example 2:

/he

Linux Vi Searching10

Look at the above snapshot, after searching for he with command /he, cursor has highlighted he from the word.

/pl[abc]ce

This command is helpful when you are in doubt with the spelling of a word. Here you can write all the possibilities related to your search.

Example:

/cont[ae]nt

Linux Vi Searching11 Look at the above snapshot, after typing command /cont[ae]nt, word content is highlighted.




Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA