Transforming & Substituting Text
The course is part of this learning path
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.
One of the compelling reasons to learn Vim is that it's so ubiquitous. It's practically everywhere. I can't ever recall logging onto a Unix or Linux system and not having access to either VI or Vim. Also as a system administrator, I often find myself connecting to a server and quickly editing a configuration file. And of course, I invariably end up using Vim to make those configuration changes. And when I'm making such a change, I'm typically looking for a specific thing to change. And this means I need a way to quickly search within a file to find exactly what I'm looking for. If you're a developer, you'll find yourself in a similar situation. For example, let's say you have a bug to fix. Well, typically, you know where to start looking. For instance, you might know the name of the function the bug is associated with or you might know the name of the variable that's key in that bit of code that's causing you troubles or whatever. And if we zoom in a little bit, you might have noticed a typing mistake on the line you're editing. It would be nice to have a way to quickly jump directly to what you're looking for on that line. With all these examples, you have an idea of what to search for. So, being able to quickly find what you're looking for is super valuable. Let's open the searching.txt file for this lesson. 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. I'm gonna go to my downloads directory, then Vim class. And now I use Vim to edit searching.txt. As always you can follow along with me now or wait for the practice exercise that will follow. Let's start with line-wise searching. To search forward or to the right on the same line use F followed by the character you're searching for. For example, if you wanna move your cursor to the next occurrence of the letter B on the line type FB. After you type FB, you'll find your cursor under the B and the word by on this line. Let's say you want to move your cursor to the letter capital A. To do that type F shift + A. Notice how this is case sensitive. If you were to type F capital A again, I'll do that here F capital A your cursor won't move because there isn't another capital A on this line. Using FA with a lowercase A, we'll do that now, FA does move the cursor forward because there is a lower case A on the line and front of the current cursor position. If you wanna search backwards on the same line use capital F followed by the character you're looking for. So you can place your cursor under the Z in zebra by typing shift + F and hitting Z. To move your cursor under the period, you can type shift + F period. To move your cursor to the beginning of the line, you can use capital F capital S. Let's do another forward search with FA. We can have FA and we're jumped to the first occurrence of A in this line. Now we can repeat this search by typing FA again. However, you can also the semi-colon to repeat the search. So as you keep pressing semi-colon which I'll do here, the cursor keeps advancing to the next the search result. If there are no more search results on the line then the cursor will stay where it is. So we can do this. I'll just hit to the last A on the line. And if you keep hitting the semi-colon the cursory doesn't move anywhere because there are no more instances of your search on your particular line. Now, if you wanna repeat this search but in the opposite direction, you can type a comma. So let me do that, hit comma. It goes back to the previous A, comma again back to the previous A, and then we can keep doing this until we're back to the beginning of the line. Again, hitting a comma when there are no more search results doesn't move your cursor. So know that the semi-colon repeats the search in the same direction that it was started in and the comma repeats the search in the opposite direction that the search was started in. If you wanna position your cursor one position before a given character use the till command which is lowercase T followed by the character you're searching for. For example, if you type TI, the cursor moves to the position before the letter I which in this example is the letter H. If you were to repeat that command your cursor wouldn't go anywhere because it is already one position before the search for character. So let's do TI again, and it doesn't move you because you're already one character before the I. I'll just move over a couple of times with L and then I'll type a TI, again, now your cursor is under the space before the next I on the line. If you wanna redo the search without having to change your current cursor position, use the semi-colon so I'm gonna hit the semi colon now, and then you can see that our cursor is positioned under the L just prior to the next I and the line. And I'll keep doing this here. Another semi-colon puts us under the P which is before the next I on the line. You can also perform a reverse or backwards til search by using the capital T command. So when you type capital T lower case A, your cursor is positioned just after the A in the line. Likewise you can use the semi-colon to repeat this search too. So typing semi-colon puts your cursor under the letter R in the word forward. To go the other way remember, that you can use the comma. So typing comma in this case is just like typing TA. So I'll hit comma again and now it's just like we had typed TA. Of course you already know how to repeat a command by starting it with a number so if you were to type 2F space, your cursor would jump directly to the second space it found on the line. So again, you can use count with these commands as well. By the way, these are considered motions. This means you can combine these motions with other commands you already know. So it's just the D command to delete, C command to change, or the Y command to yank. Now, let's move down to the next line of text here. I'm going to position my cursor at the very beginning of the line with zero. And now let's practice this motion that we've just learned. So we can use F shift T to bring us to the first occurrence of the capital T on this line which is at the beginning of the word this. Now to delete everything from your current cursor position up until the letter W, use DTW. Let's undo that with U. Can you think of another way to do the same thing? Sure. You can use DF space. That deletes forward up to and including the next space on the line. There's another way that we could have done the same thing using D uppercase W. Now that is perhaps more efficient for this particular example, but what really matters is that you know how to think in Vim. This way you have options. You can go for super efficiency, or you can just use what you can remember and what feels right to you. So that's how you search on the same line. But what if you wanna find something that may or may not be on the same line, or what if you want to search for an entire word or series of characters instead of just one single character? Well, that's when you can use the forward slash search command, simply type forward slash followed by whatever it is you're looking for. For example, to find the next occurrence of the word and, type forward slash A-N-D and press Enter. To repeat your Ford search type the letter N for next. So we can keep hitting N and we keep jumping to the next word and. If you keep pressing N eventually the search will wrap back around to the first occurrence, and you'll see a message at the bottom of your screen that says, search hit bottom, continuing at top. And I'll do that here. Keep hitting N. And there it is. Eventually we've wrapped around. We've hit the bottom of the file and now we've continued back at the top of the file. If you wanna repeat the same search but in the reverse direction, use capital N. So let me hit shift + N, and then I'll keep doing that and it keeps going to the occurrence of the characters AND. By the way, you can start a new forward search at any time to search for the characters FOR we can just hit the Ford slash FOR and press Enter and now we have a new forward search. If you're seeing something slightly different than what I'm demonstrating here it's probably due to a couple of settings. The first setting is IS, which is short for INCSEARCH, which stands for incremental search. Now I have this option turned on. And to check if you do or not, you can type the command colon SET space IS question mark and hit Enter. If it is on you'll see what's on my screen at the bottom and the status line, which says INCSEARCH. Now if it's turned off, you'll see no incsearch instead. Now you can explicitly turn it on with colon set, S-E-T space IS and press Enter. Now I'm going to position my cursor at the top of the file with GG. Now watch closely as I perform a forward search. So I'm gonna type forward slash A. Do you see that the A is being highlighted? My cursor hasn't moved yet because I haven't hit Enter. But Vim has shown me where I would end up if I did hit Enter right now. Now okay. Let's keep typing. I'll add an "an" to my search. Now, the nearest match of AN is highlighted. If that's where you want to position your cursor hit Enter which I'll do now and that is where your cursor is placed. Let's do the same thing again but this time where the incremental search setting off. And to turn it off, use colon SET space no IS and press Enter. And you can check to make sure it's off with colon SET IS question mark and it says noincsearch. So it is disabled. Okay. So I'm back at the top of the file we go with G G. Now, when I type forward slash A, nothing on my screen is highlighted. I'll keep typing by adding an N and then pressing Enter. Now our cursor moves to the nearest match. So that's the incremental search option. Another option that can affect how searching appears is the highlight search option represented by HLS search or HLS for short. To check this setting we can type colon set space HLS question mark and press Enter. So it says no HL search. So that means it's disabled for me. Now let's turn it on and see what it does. So we can type colon set, HLS and press Enter. We can check it with set HLS question mark and then it says, HL search is enabled. Now, all those matches that we previously searched for are highlighted. Now let's try a new search. Let's do forward slash THE, and press Enter. Now, all those matches are highlighted. Let's move to the next line with J. Notice how the matches stay highlighted. They'll stay highlighted until your next search or you temporarily turn them off with a colon noHLS command. Let's do that now. I'll type colon, noHLS and press Enter and those highlights are gone from your screen. Now this doesn't disable the HLS option. This only disables highlighting until the next search. So let's do another search with forward slash IN, and press Enter. Now, all these matches are highlighted. To disable search highlighting use set noHLS and press Enter. Now, when you do future searches nothing will be highlighted. So again, if you see something slightly different it's probably due to the incremental search option which is IS or the highlighting option which is HLS. To learn how to save your preferences, see the lesson on the VIMRC file and Vim settings. We'll go into great detail about these settings there. Okay. So let's get back to the point of this lesson, which is searching. Now, I'm going to position my cursor back to the beginning of the file with GG. And now I'm going to search for the word and with forward slash AND and press Enter. I wanna point out a common pattern I find myself using, and I think you'll end up using it too. And that is performing a search such as forward slash N like we've done here, executing a command, let me execute one I'll do CW to change word, and I'll change this word to an ampersand and then pressing Escape. And the next part of the pattern is using N to move forward to the next match and using the dot command to repeat the previous command. So I hit dot here, and then I'll do this again. I'll go N for the next instance of N place dot and I'll repeat the CW command that we've just ran. And then I can keep doing this as many times as I want. If I need to make four or five quick edits I'll use this pattern of searching, performing a command, moving to the next occurrence with N, and repeating that command with dot. Now in just a bit you'll learn how to do a global find and replace which can often do the same thing, but again for quick edits this method works fairly well. Okay. Again, back to searching. So to start a backwards or reverse search use question mark followed by the pattern. If you wanted to find the characters IS that come before your current cursor position, just use question mark IS and press Enter. Again you can use N to repeat the search in the same direction that you search for. And then you can also use capital N to reverse the direction of the search as well. I'm gonna hit N a couple of times here and it keeps searching backwards. We loop there, and then I'll use shift + N in which is capital N to search in the opposite direction. So you can think of shift + N in this instance as reversing the reverse search, which of course means it's forward. Let's position our cursor back on the first line in the file and place it under the word is. So I'll go GG and then I'll just hit W a couple of times here to move into position. Now to search for the next occurrence of the word under or nearest the cursor type an asterisk. So I'll hit an asterisk. And if you keep typing asterisk, which I'll do again, it moves to the next occurrence of the word. And you can also use N to accomplish the same task. So if I hit and a few times, then it keeps the searching for the word that we first started searching for. Again use capital N to reverse the search. So I'll do that hit capital N and now we're going in the reverse direction. You may notice that the characters IS appear other places in the file such as in the words accomplished, episode, and sisters. If you wanna find those characters use the forward slash search or the reverse search. The asterisk is meant for matching an entire word. Let's go back to the first line again and position our cursor under the word by. So I'll do GG, and then I'll just do FB to position our cursor under the word by. To perform a backward search for the word nearest or under the cursor use the pound sign. So I'll do shift + 3 on my keyboard and again, I can keep hitting shift + 3 which is the pound sign to do this backward search or I can hit N to keep searching in the same direction, of course, shift + N or capital N reverses the direction of the search, which, you know, we talked about this before. A reverse of a reverse is actually forward. You can use these search motions with commands, such as D, C, and Y. For example, you can easily delete everything from your current cursor position all the way until the last line by typing D forward slash this. So let's just move our cursor up a couple of lines here and do D forward slash THIS, and press Enter. And so everything from our current cursor position up until that search result was deleted. I'm gonna hit U to undo that deletion. Let me jump to the beginning of the file here with GG. Also note that you don't have to make these big motions. You can do something even on the same line such as double quote A, Y, forward slash Z. That gangs all the texts from your current cursor position up to the next occurrence of the letter Z. If we type REG A you can see that the text was yanked into the A register.
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.