Contents
Type : (colon) followed by %s/foo/bar/ and hit [Enter] key. Above command will replace first occurrence of word foo with bar on all lines. The % is shorthand for all lines.
How do you search and replace in vi in Unix?
Searching and Replacing in vi
- vi hairyspider. For starters, access vi and a specific file.
- /spider. Enter command mode, then type / followed by the text you’re looking for. …
- to find the first occurrence of the term. Type n to find the next one.
How do I do a global replace in vi?
The % is a shortcut that tells vi to search all lines of the file for search_string and change it to replacement_string . The global ( g ) flag at the end of the command tells vi to continue searching for other occurrences of search_string . To confirm each replacement, add the confirm ( c ) flag after the global flag.