Lesson 9 of 26
Basic Search and Replace in a file
In this lesson
Roy uses the substitute command, typed as a colon, s, the search pattern and the replacement between forward slashes, to replace text on the current line, and applies it to several lines by selecting them with Shift-V first. Only the first match on each line is replaced unless g for global is added at the end. He also shows a range that covers the whole file and the c flag, which asks for confirmation (yes, no, all or quit) at each match.
Transcript
[00:07] (external link, opens in new tab) Let's talk about search and replace inside a file. First the basic commands for searching are acting on the current line. So if I want to replace the letter a in the current line which is the first line. What I want to do is in the command line type colon and then of course I want to substitute. So I want to well I just in my head I say s for search and then forward slash I want you to search for the letter a.
[00:39] (external link, opens in new tab) So n forward slash that's the search regular expression basically and I want you to replace it with ttt for example and if I just do that what happens is that the letter a changes. However this is only for the current line. So how do I make it so that it applies to two lines? Well there are two ways. Technically before the s you can type in the starting and ending line or the easier way is to move to visual mode (external link, opens in new tab) shift v and let me undo that first shift v
[01:16] (external link, opens in new tab) And select let's say these three lines and then type colon. And now you'll see at the bottom here is that this is actually indicating the current highlighted search current highlight highlighted text. So I'm going to type again s. So search for a replace it with ttt. Now what happens now is something a bit weird is that it search for all the a's but only the first ones.
[01:50] (external link, opens in new tab) Let's try that again. This time I'm going to change the letter e. Okay. Just select this and then type search for E and change it to Y Y. And what we we're supposed to see here is that only the first E have been changed. For example, in the first line here. Let's try that again just to make sure.
[02:21] (external link, opens in new tab) I'm going to press undo. And let's try the same thing with the letter M. Shift V. Search for M. Replace it with UU. And what you'll see is that only the first search in each line is actually being replaced. So if I want more than the first or basically all of the found characters that match the regular expression to actually be replaced, I have to append g for global at the end.
[02:55] (external link, opens in new tab) So if I select this I type search for in this case m replace it with uu and I'm going to add just this extra slash to say do that globally and that will replace all of the found matches. Now I can also say if I wanted to do this throughout the file, I could technically go GG up, shift V, and then shift G to select all the file and do my search and replace.
[03:33] (external link, opens in new tab) But there's also a shortcut which is the file name. So I can say the file name that's the range and again substitute all of the E characters with I I I and do that globally. And now this is going to work throughout the file. One last thing is that sometimes you want to get a confirmation
[04:04] (external link, opens in new tab) For each change. So then you can technically I'm just going to say colon and then go up to repeat and you just add the letter C for confirmation. And now for each match you see at the bottom here you have the ability to say yes or no or all or quit to stop etc or just for this line or until the end of the line. So for us, I can just say yes, yes, no, yes, yes, and all from now on.
[04:36] (external link, opens in new tab) So that's the simplest way to get that stuff working.