From the course: Learning vi

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Searching and replacing text

Searching and replacing text - vi Tutorial

From the course: Learning vi

Start my 1-month free trial

Searching and replacing text

Now, we'll talk about search and replace. Search and replace in VI is a little bit more complicated. Search and replace in VI uses a colon command, :s. The way that you specify a search and replace is you type colon s, a slash, the old string that you're looking for, another slash, the new string that you want to replace it with, and then a final slash to end the command, then you enter return to execute it. That command :s/ old / new / replaces the first occurrence of the string old on the current line with new. If you want to replace all occurrences of old with new on the current line, you append a g after the closing slash, :s / old / new. If you want to make that search and replace on every line of the file, you put a percent sign between the colon and the s, :%s/ old / new /, that replaces the first occurrence of old with new on every line of the file. You can combine both of those options, :%s/ old / new, replaces every occurrence of old, with new, on every line of the file. Now…

Contents