vi Find And Replace Text Command

htt‮‬ps://www.lautturi.com
vi Find And Replace Text Command

To find and replace text in the vi text editor, you can use the :s command, also known as the substitute command. The :s command allows you to search for a specific pattern of text and replace it with new text.

To use the :s command, you can enter the following syntax:

:s/pattern/replacement/

This will search for the pattern of text and replace it with the replacement text.

For example, to replace all occurrences of the word "apple" with the word "banana" in the current file, you can use the following command:

:s/apple/banana/

You can also use the g flag to replace all occurrences of the pattern in the entire file, rather than just the first occurrence on each line. For example:

:%s/pattern/replacement/g

This will replace all occurrences of the pattern in the entire file with the replacement text.

You can also use the i flag to perform a case-insensitive search. For example:

:%s/pattern/replacement/gi

This will replace all occurrences of the pattern, regardless of case, with the replacement text in the entire file.

Keep in mind that the :s command operates on a single line by default. If you want to replace text across multiple lines, you can use the % range to operate on the entire file.

You can also use the n flag to preview the changes without making them permanent. For example:

:%s/pattern/replacement/gn

This will display the changes that would be made by the :s command, but will not make the changes permanent.

By using the :s command with various flags and options, you can find and replace text in the vi text editor. You may want to refer to the vi documentation or online resources for more information about the :s command and other text editing features of vi.

Created Time:2017-10-30 14:27:36  Author:lautturi