Chat Zalo Chat Messenger Phone Number Đăng nhập
Searching and Replacing With vi

Searching and Replacing With vi

VI provides several ways to find your place in a file by locating a specified string of characters. VI also has a powerful global replacement function.

Find a

character string

A character string is one or more characters in succession. A string can include letters, numbers, punctuation marks, special characters, white space, tabs, or carriage returns. A string can be a grammatical word or it can be part of a word.

To search for a character string, type / followed by the string you want to find, and then press Return. vi places the cursor on the next occurrence of the string. For example, to find the string “meta”, type /meta followed by Return.

Type n to go to the next occurrence of the string. Type N to go to the previous occurrence.

To look back on a file, you can use ? instead of /. In this situation, the n and N addresses are reversed.

Searches are usually case sensitive: a search for “china” will not find “china”. If you want vi to ignore case during a search, type :set ic. To change it back to the default case-sensitive mode, type :set noic.

If vi finds the requested string, the cursor stops at its first appearance. If the string is not found, vi displays Pattern Not Found on the last line of the screen.

Certain special characters (/& !^*$?) have special meaning for the search process and must “slip through” when used in a search. To escape a special character, precede it with a backslash (). For example, to search for the string “anything?” type /anything? and press Return.

You can use these special characters as commands for the search function. If you want to find a string that includes one or more of these characters, you must precede the special character with a backslash. To escape a backslash, type \.

Refine

your search

You can make searches more accurate by tagging the string with flags for the following characteristics:

Beginning of the line End of line Start of word End of

  • word

  • Wildcard characters

To match the beginning of a line, start the search string with a caret (^). For example, to find the next line that begins with “Search”, type:

/^Search

To match the end of a line, end the search string with a dollar sign ($). For example, to find the next line ending with “search.”, type:

/search.$ Note that the

point escapes with a backslash

. To match the beginning of a word, type < at the beginning of the string

; To match the end of a word, type > at the end of the string. Therefore, to match a word, rather than a string, combine the end-of-word and end-of-word tags in the search pattern. For example, to find the next occurrence of the word, instead of the string, “search”, type:

/<search> To match

any character, type a period (.) in the string at the location you want it to match. For example, to find the next occurrence

of “disinformation” or “disinformation,” type: /.isinformation Because this is a search for a string and not a

word, this search pattern can also find constructs like “disinformation” and “disinformation.”

To search for alternate characters in a string, enclose the alternatives in square brackets. The /[md]string search pattern finds strings that begin with “m” or “d”. In contrast, /[d-m]string finds strings that begin with any letter from “d” to “m”.

To match zero or more occurrences of the last character, type an asterisk (*) in the string. You can effectively combine square brackets and an asterisk to find well-defined alternatives. For example, to find all strings that begin with a through z and end with “isinformation” and to find all occurrences of the string “isinformation”, type:

/[a-z]*isinformation

Replace

a character string

The procedure for replacing a text string is based on the search procedures described above. You can use all special matching characters for search and replace searches.

The basic command form is

: :g/search-string/s//replace-string/g Then press the Return key.

Therefore, to replace each occurrence of the string

“disinformation” with “newspeak”, type: :g/disinformation/s//newspeak/g

Then

and

press Return

.

You can modify this command to stop the search and make vi query if you want to replace on each instance. The following command uses gc (adding c for “query”) to make vi stop at every occurrence of “misinformation” and ask if you want to make the substitution. Answer with and for yes or n for no.

:g/disinformation/s//newspeak/gc Note:

You can cancel a “consulted” find and replace function by pressing Ctrl-C

.

Go to a specific

line

To go to the last line of an open file, type G. To return to the first line of the file, type 1G.

You can go to any other line by typing your number followed by G.

For example, suppose you exit the file painting while editing line 51. You can access that line by opening the file and typing 51G.

Contact US