How to Search & Replace in vi

Press the escape key to ensure that vi is in the command mode., Type a colon., Type the command %s/string/replacement., Press the return (enter) key.

4 Steps 1 min read Easy

Step-by-Step Guide

  1. Step 1: Press the escape key to ensure that vi is in the command mode.

    This will will place the cursor on the last row of the window and present a colon as a prompt. , Replace "string" with for what you are searching and replace "replacement" with what to replace it.

    For example "%s/&/and".

    The preceding "%" character tells vi to search the whole document.

    Without it vi will only search and replace on the current line. , The string will be replaced with the replacement in the whole file.

    And the prompt will go away to allow you go into insertion mode immediately.

    The cursor will be at the beginning of the last line that was changed.
  2. Step 2: Type a colon.

  3. Step 3: Type the command %s/string/replacement.

  4. Step 4: Press the return (enter) key.

Detailed Guide

This will will place the cursor on the last row of the window and present a colon as a prompt. , Replace "string" with for what you are searching and replace "replacement" with what to replace it.

For example "%s/&/and".

The preceding "%" character tells vi to search the whole document.

Without it vi will only search and replace on the current line. , The string will be replaced with the replacement in the whole file.

And the prompt will go away to allow you go into insertion mode immediately.

The cursor will be at the beginning of the last line that was changed.

About the Author

D

David Cooper

Enthusiastic about teaching hobbies techniques through clear, step-by-step guides.

36 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: