Transforming & Substituting Text
This course covers how to transform and substitute text in Vim. We look at how to insert, replace, change, and join texts through a range of commands, and then you'll have the opportunity to try these out by following along with a guided demo.
Then we move on to searching, finding, and replacing text within files, and once again, they'll be a guided walkthrough to show you the real-world application of these features.
This course is ideal for anyone who needs to edit text files in a command-line environment.
If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.
If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.
In an effort to get you quickly editing files early on in the course, I just showed you the 'i' command to enter insert mode. Well, as you might've already figured out on your own, there is more than one way to enter insert mode. It shouldn't surprise you really. We've seen this time and time again with Vim, where, there is more than just one way to do the same thing, and where there are multiple commands that do something very similar to each other. As usual, I'm going to use a file here, so I can demonstrate some of these other ways to get into insert mode. First, I'm going to open up a terminal on my system and navigate to where I extracted the contents of the course download archive, and for me, that's in the Downloads folder, in the vimclass folder. And now I'm going to open the file called inserting.txt. Let's say you wanna make the second line in this file just like the first line. One way to do this is to move your cursor to the second line and position it where there is some missing text, just in front of the word 'is'. So I'll press 'j', and I could press 'w' to move over a couple of words here. And now, you can press 'i' to enter insert mode and type the word 'lines', followed by a space and then press ESC to get to normal mode and then move over again to the next place. Hit 'i' to go at into insert mode, type the word 'not', press ESC again, to get back to normal mode. Now, this is pretty much the method you already know. So, now let's move down to the line that begins with 'my'. Just do that by pressing 'j' three times here or if you're going for efficiency you could have just used 'three j'. Now, to make this line look like the one above it, you have a few choices. If you were to use the method you already know, you would have to position the cursor at the beginning of the line and then enter insert mode. To position the cursor at the beginning of this line, you could use 'zero' or the caret symbol, which is 'shift six' on my keyboard. Both are the same because this line starts with a non blank character, but remember that the caret symbol moves to the first non blank character of the line. From there, you can just enter insert mode with 'i'. However, there is a single command that does both of those things. It's capital "I" or shift "I". So when you type shift "I", which I'll do right now, shift "I", the cursor jumps to the first non blank character in the line and you're placed into insert mode. This is the same thing as pressing caret "I". So the lowercase "i" command lets you insert text before your current cursor position and capital "I" lets you insert text before the first non blank character on the line you're on. To make this line just like the one above it, you can just type capital H E L L O, 'comma', 'space', and then press ESC to return to normal mode. Now let's move down to the line that begins with 'o c e' and I can do that with 'three j' and then press 'zero' to go to the beginning of the line. Now let's say you want OCE to read 'once', O N C E. You can move the cursor over a character and use the "i" command to enter insert mode. Or you can simply press the "a" command from your current cursor position. And we'll do that now and press 'a'. And what happens is that the 'a' command appends text after the current cursor position. So, to fix this mistake, we just type 'a', like we did, hit 'n' to fix our mistake, press ESC to return to normal mode, and we're all set. Again, let's jump to the bottom line of the next pair using 'three J'. Now this line is missing the last word in the sentence. So you can use the capital "A" command which appends to the end of the line. So, you can easily make this line match the one above it by first pressing 'shift a' and then typing 'space, d a y', pressing 'period', then pressing ESC to return back to normal mode. So again, capital "A" is append to the end of the line. Now let's move down to the next non blank line here. We can just move down with 'j' and 'j' again. And this line reads- "What is 2+2? Answer on the line below" To do this, use the 'o' command lowercase 'o'. It begins a new line below the cursor and places you in insert mode. So to answer this question on the line below, you would type 'o', press 'four', and then press ESC to return back to normal mode. Now let's do this again, let's move to the next non blank line here with 'two J' and let's say you want to create a line above the current line and immediately start inserting text on that line. Well to do that you would use capital 'O' or shift 'o'. So to answer this question on the line above, I'm going to type 'shift', 'o', which starts a new line above the current cursor position. I'm in insert mode, so I can just type 'four' to insert the text, press ESC, and now we're back to normal mode. Next, let's move down a few more spaces here. I'm going to position my cursor below this line that says create a line of asterisks and I'm going to hit 'Z' and 'enter', to just move the text up our screen. You already know how you can make a command repeat as many times as you want it to by proceeding that command with a number which is called a count. This also works with the insert command. It may not be obvious at first, but it's pretty cool. So if you want to create a line that contains 80 asterisks, then you just type 'eight' 'zero' 'i', and now you're an insert mode, make your edit, which is simply pressing 'shift eight' to enter an asterisk. And now when you press ESC, that command is repeated 80 times and you're left with a line of 80 asterisks, right there in your editor. Okay, now let's move down to the next line. I'll just hit 'j', and let's say you want to create five new lines that begin with the 'pound' sign or some people call it a number sign or a hash. Think about how you would create one new line below your current cursor position. That's right, You can use the 'o' command to do that. Now if you were to run 'o', 'pound', ESC, you would have a new line that contained the 'pound' sign. Let's do that five times by typing 'five' 'o', now you're placed in insert mode. Hit the 'pound' sign here and then press ESC. This might be a quick way to create a comment section in a configuration file, shell script or other program. By the way, you can repeat these commands with more than just one character. For example, let's say you want to create a list of IP addresses and they all start with 10.11.12. Well, to create four lines that start like that, we can just go down here and type 'four', 'o', 10.11.12 'dot' and press ESC. And now you can quickly go back and fill in the last section of each IP address, because you created four lines really quickly that started out the same way. There is a very similar mode to insert mode in Vim, and it's called replace mode. Some would actually argue that it's just really another form of insert mode. In any case, when you enter replace mode, each character you type replaces an existing character. Let's try this with a line that reads "Replace me!". So first we'll position our cursor where you want to start to replace. And so we'll go down here, and then I'm going to press "zero" to jump to the beginning of the line. And then now what we can do is type capital 'R' or shift 'R', you'll see replace appear on the status line at the bottom of your screen indicating that you're in replace mode. Now, whatever you type will overwrite the existing text. So let's try... "I love using Vim exclamation mark". And then when we're done, we can press ESC. And now we're returned to normal mode. And this example, we typed over the characters and even beyond the original end of the line. So in replace mode, one character in the line is deleted for every character you type. If there is no character to delete like at the end of the line, then that type character is appended to the line. It's actually pretty intuitive. Once you use this once or twice you'll really understand how that works. So let's use replace mode to change the word 'dog' to 'cat' in the next line. So I'll just go down a couple here and then go back a couple of words by using 'b'. Now we can type shift 'R', capital 'R' and then C A T, and press ESC, and we're back into normal mode and we replaced 'dog' with 'cat'. If you just want to replace one character, use lowercase 'R', so in the next sentence let's change 'bat' to 'cat'. So we'll position our cursor under the 'b', just move down here, and go back to the beginning of the word, type lowercase 'r' followed by 'c' and the 'b' is replaced with 'c'. Now notice how we didn't have to hit ESC to go back to normal mode. Lowercase 'r' only lets you replace one character. So you just hit that one character and then you're automatically placed back into normal mode. Okay, let's move down to the next line that has some text here. And let's say you want to replace word 'canine' with the word 'dog'. You could use the replace mode but after you replaced the first three characters, you would have to delete the remaining characters in the word since it's longer than the words you want to replace it with. This is where the 'c' or change command is useful. The format of the 'c' command is an optional register, followed by 'c', followed by a motion. So to change a word, we could type 'cw'. So let's do that here with 'canine'. You can position the cursor right at the beginning of the word 'canine', type 'c' for change, 'w' for a word motion, now we're into insert mode. And now we can just type cat 'c a t', press ESC, and that word was changed from 'canine' to 'cat', pretty easy. What essentially happens is the word is deleted and you're placed in insert mode. So whatever you type replaces the motion you specified, it doesn't have to be equal either. So let's move down to the next line. Go 'b' to go back to the beginning of this word and this time let's change 'canine' to 'black cat'. Again, you can use 'cw' and then type your replacement text. So we'll do 'cw' to change the word 'canine'. We're going to type 'black cat' and press ESC. So you can quickly change a single word into multiple words or multiple lines or whatever replacement texts that you want, when you're using this 'c' motion command. All right, let's do this one more time, let's change 'canine' to 'cat'. And this time what we want to do is save it into the 'a' named register. So, we'll position our cursor at the beginning of 'canine', I'll just hit 'b'to go back here and then we'll type double quote a, 'c w', type 'cat', and then hit ESC. Now we can look, look at the registers with a register command we'll type 'R E G' 'space' 'a' and press enter. And you'll see that the text that we replaced is now in the 'a' register, the word 'canine'. You already know how to use registers, but I just wanted to give you a little reminder using this new command that you were just introduced to. Let's move down a couple of lines here and I'm just going to shift this text to the top of our screen here with 'z' and 'enter', so we can see what we're doing. Let's say you want to replace all the text on the line starting at the word 'car'. So to do this, just position your cursor at 'car' and I'll just do this with 'w' 'w' over to 'car' and then we can type 'c' '