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.
The goal of this exercise is to give you some practice with the different ways to insert, change, replace and join techs. First off, start a command line session on your local machine, and then next use Vim to open the file named insert-practice.txt. And that file came with the course downloads. And to do that, you need to navigate to the location of that file, where you extracted your course downloads. And remember, this could be slightly different for you but for me, it's in my Downloads directory. So I'm going to start out by going cd Downloads and then enter the vimclass directory with CD vimclass. And now I'm going to type Vim insert-practice.txt and press Enter. I'm sure that you remember that the lowercase I command places you into insert mode right at your current cursor position and we're just gonna practice that briefly. So we're just going to use this command to insert some texts at the very beginning of the first line here in the file. For example, you might answer this question. "What is your favorite editor with the answer Vim?" And to do that, we had press I to go into insert mode, type V-I-M, we'll put a space there to make it look pretty and hit Escape to return to normal mode. Now let's work with the uppercase I command and let's move to the next line of text by typing 2J. Now your cursor will be in the middle of this line. So if you wanna answer texts at the very beginning of the line you use the capital I command and let's do that here. I'll do Shift+I for capital I, now we're at the beginning of the line and placed in insert mode and we'll go ahead and answer this question here. "What is your favorite color?" Let's say it's blue. So I'll type B-L-U-E, space to make it look pretty. Hit Escape to go back to normal mode. Next let's position our cursor on the next line of text here by typing 2J and what we wanna do is just insert our names in between those greater than and less than symbols on the line. And to do that, where we're going to do is use F greater than to move our cursor forward to the greater than symbol here and then we can use the a command, that's lowercase a to start insert mode just past or just to the right of our cursor position and then we can type our name. For example, my name is Jason, so I'll type Jason and press Escape to return to normal mode. Now let's move to the next line here with 2J. Now we're going to append our name to the end of this line with a capital A command. And currently with my example where my cursor is I don't necessarily have to use the capital A command since my cursor is already at the end of the line. So just to make this clear, I'm going to hit zero first to position my cursor all the way at the beginning of the line. And we can assume, for example that you would want to enter text at the far end of the line when you're at the beginning or in the middle of that line. So now what I'm going to do is hit Shift+A to inner insert mode at the very end of the line and I'm just gonna type my name here, Jason and press Escape to return to normal mode. Now we're going to position our cursor on the line of text. Again, we can use 2J to do that and then we're going to enter insert mode on the line below this line by typing lower case o. Next type the name of your favorite movie, whatever that is I'm going to pick "THe Wizard of Oz" and now I can hit Escape to return to normal mode. Again, we're going to move to the next set of lines with 2J and now what we wanna to do is enter insert mode on the line above the current line and we can do that by typing Shift+O or using the capital O command, the same thing there. And so now what we can do is type V-I-M and press Escape to return to normal mode. Now we're gonna jump to the next line of text. I can see it's three lines below so I'm gonna type 3J. For me I wanna move the view up. So I'm gonna hit Z and Enter to do that. And let's say, we want to join these two lines together and you can do that with the uppercase J command. So I'm gonna hit Shift and J and that lower line is upended to our current line. So it joins those two lines. Next let's work with the uppercase R command, which is replace. So let's place our cursor on the next line that has text in it which we can go there with 2J. Now let's replace "her" with "our" and we can do this by using a backward search capital F H and that will put us under the H in her. Now we can inter replace mode with Shift+R capital R and type our O-U-R and press Escape to return normal mode. Let's jump to the next line of text here with 2J and here we wanna change just one character. We wanna change "car" to "cat". So we just wanna substitute the letter R with the letter T. So let's move forward in this line to the letter R. So I'll do F R and now we use the R command to replace this one character, I'll pit R now and then we're going to type our character which is T and now R has been replaced with T and there's no need to hit Escape to return to normal mode because of the lowercase r command, you only substitute one character and once you type that one character you are automatically placed back into normal mode. Now let's practice the C command by changing the word "great" to "brilliant" on the next line of text. We can position our cursor under G in the word "great" by performing a forward search with forward slash G R and pressing Enter. Now type CW which stands for change word and now we can type brilliant, B-R-I-L-L-I-A-N-T and finally, we hit Escape to return to normal mode. Now let's change the word "myself" to "everyone" on the next line of text. And again, we can use a forward search to get there quickly. So I'll do forward slash my, and press Enter. And now I'm going to change the word including the punctuation with C capital W and that allows you again, to change the word including any punctuation that follows it. So now we're going to type is "everyone!" with an exclamation mark and Escape to return to normal mode. Let's place our cursor on the next line of text here with 2J and let's replace this entire line and you can do that with CC. So this allows us to change the entire line and I'm just gonna type something fun here like "The sky is beautiful!" When I hit Escape, we're returned it back into normal mode. Now it's practice changing case with the Tilda command or switch case command. Now let's capitalize the first letter in the word "Monday" so we can get there quickly by doing a forward search of slash M and hit Enter. And I just wanna move this up on the screen so I'm gonna hit Z_Enter to reposition the text higher in our view here. Now, all I need to do is type a Tilda Shift and hit the Tilda there and the case switch operation was executed. Now let's capitalize the word "Shout". Let's get there quickly by doing a forward search forward slash SH and hitting Enter. And now we can type G Tilda W and that performs the case switch operation on the word "motion". Now let's switch the entire case of the following line. And so we can get there with 2J and we can run G Tilda Tilda and that switches the case of the entire line. Now let's work with a capital U command on the next line of text here. Let's change the word "shout" to all uppercase. And so we can get there quickly by doing a forward search, forward slash capital S and pressing Enter. And now we can use G uppercase W and that capitalizes that word "MOTION". Just for the fun of it, we're going to jump to the next line here with 2J and we're going to do the same command again, this time just using the uppercase W motion. It should do the same thing. Always keep in mind that there are more than one way to accomplish the same task. So I'm gonna type G uppercase U and uppercase W and sure enough, it does the same thing. So if you wanna think that way, that's fine. Use uppercase W or lowercase w in this situation both give you the same result. Now let's position our cursor under the capital W of the word "Whisper" by doing a forward search. So forward slash W press Enter and next we're going to type G u W and notice that was a lowercase u. And what that does is performs a lowercase operation on the motion that we specified which again was the word "motion" so it made this word all lowercase. Now let's try repeating a couple of commands here. So on the line that says, "Create a line of asterisks below". We're gonna follow those instructions and do just that. So I can see that it's three lines down so I can go 3J to get to that blank line under that instruction line, if you will. And now what we're going to do is supply a count to insert mode. So we're going to type e80 I and next we're just gonna type our asterisk, Shift+8 on my keyboard, press Escape, to return to normal mode and it fills out a line of 80 asterisks. So let me hit Z and Enter so that you can see that entire line on the screen here. Now, let's go to the next line of text here with 2J. And what we wanna do is just create three lines that start with a dash. And you might use this, for example when you're creating a gamble file or you're just making a to-do list for yourself or some other type of reason like that. So here, we're going to supply a count to this command as well. And we'll do that with three lowercase o which puts us in insert mode one line below where we're at, we'll type a dash or a hyphen as some people call it here, and then I'll hit Escape and that command gets repeated two more times for a total of three times. At this point, I always want to encourage you to try some exercises on your own, open up some files that you have that need to be edited and practice your techniques that you've learned here in this lesson and in this Practice Exercise. And if you're done with the file and perhaps wanna come back and run this exercise again you can quit without saving your changes by typing :q! and pressing Enter.
Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.