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.
Step-by-Step Guide
-
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. -
Step 2: Type a colon.
-
Step 3: Type the command %s/string/replacement.
-
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
David Cooper
Enthusiastic about teaching hobbies techniques through clear, step-by-step guides.
Rate This Guide
How helpful was this guide? Click to rate: