1.

How Do You Do A Search And Replace?

Answer»

Well, there are a few methods. The simplest is:

:s/old/new/g But, this only does it on the current line... So:
:%s/old/new/g In general,
:[range]s/old/new/[cgi]

Where [range] is any line range, including numbers, $ (end of file), . (current LOCATION), % (current file), or just two numbers with a dash between them. (Or even: .,+5 to mean the next five lines). [cgi] is either c, g, i, or nothing. c tells vi to prompt you before the changes, g to change all of the OCCURRENCES on a line. i tells vi to be case insensitive on the search. No character after the last slash will only change the first occurrence on the line.

My favorite method is:

:g/foobar/s/bar/baz/g This searches for foobar, and changes it to foobaz. It will leave jailbars alone, which the other method will not. This is my favorite method, but is harder to remember. Of course you can also use regular expression search patterns, and a few other commands in the replacement PART of the text. If you use ( and ) in the pattern to escape a sequence, you can do lots of nifty things.

For example:

:g/(foo)(bar)/s/2/1baz/g will change foobar for foobaz.

Special SEQUENCES allowed are:

& everything which was matched by the search

[1-9] The contents of the 1st-9th () pair
u The next character will be made uppercase
U The characters until E or E will be made uppercase
l The next character will be made lowercase
L The characters until e or E will be made lowercase
[eE] end the selection for making upper or lowercase

Well, there are a few methods. The simplest is:

:s/old/new/g But, this only does it on the current line... So:
:%s/old/new/g In general,
:[range]s/old/new/[cgi]

Where [range] is any line range, including numbers, $ (end of file), . (current location), % (current file), or just two numbers with a dash between them. (Or even: .,+5 to mean the next five lines). [cgi] is either c, g, i, or nothing. c tells vi to prompt you before the changes, g to change all of the occurrences on a line. i tells vi to be case insensitive on the search. No character after the last slash will only change the first occurrence on the line.

My favorite method is:

:g/foobar/s/bar/baz/g This searches for foobar, and changes it to foobaz. It will leave jailbars alone, which the other method will not. This is my favorite method, but is harder to remember. Of course you can also use regular expression search patterns, and a few other commands in the replacement part of the text. If you use ( and ) in the pattern to escape a sequence, you can do lots of nifty things.

For example:

:g/(foo)(bar)/s/2/1baz/g will change foobar for foobaz.

Special sequences allowed are:

& everything which was matched by the search

[1-9] The contents of the 1st-9th () pair
u The next character will be made uppercase
U The characters until e or E will be made uppercase
l The next character will be made lowercase
L The characters until e or E will be made lowercase
[eE] end the selection for making upper or lowercase



Discussion

No Comment Found