1. Home
  2. Training Library
  3. Cloud Computing Fundamentals
  4. Courses
  5. Transforming and Substituting Text in Vim

Exercise: Search, Find, and Replace

The course is part of this learning path

Vim Masterclass
11
6
3
Start course
Overview
Difficulty
Intermediate
Duration
59m
Students
28
Description

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.

Intended Audience

This course is ideal for anyone who needs to edit text files in a command-line environment.

Prerequisites

If you want to follow along with the exercises in this course, you should have the Vim text editor installed on your computer.

Resources

If you want to follow along with the exercise(s) in this course, you can find the necessary resources here.

Transcript

The goal of this exercise is to practice searching, finding and replacing text within a file. As always, start a command line session on your local machine. Next use of M to open the search-practice.TXT file that came in the course downloads. To do that, navigate to the location of the file. Remember that this could be different for you depending on where you extracted the contents of the course download. This example assumes that the course download was saved into your downloads folder and extracted from there. So I'm going to move into the downloads folder with CD downloads, then into the VIM class folder, and finally open the file. Press enter. First let's position our cursor at the beginning of the word four on this first line in this file and to do that, we can type F F and that's a Ford search to the next instance of F on the same line. Now let's place our cursor under the F in the word of. This is simply repeating the F F command and remember to repeat your line Y search use a semi-colon. So I'll get a semi-colon now and we are placed under the F and the word of. Now place your cursor at the beginning of the word traveler. To do that, search backwards with capital F capital T. Let's position our cursor just one character before the word B and that means your cursor will be on the space before that word. So to do that, let's use the tilt command with T B. Now let's practice by positioning our cursor on the space before the word speak. So we can use T S. Now let's position our cursor on the space after the word for. So to do that, we'll use a backwards til search with capital T and lower case R. Next let's find all occurrences of the word and and position our cursor at the beginning of each occurrence at least once and to do this, we can start out by using a Ford search with Ford slash a N D followed by a space and then hitting enter. Now notice if you didn't upend a space to the word you would have matched incandescent. So that's not the full word and, but I just wanted to point that out, that sometimes you'll need to pre-pend or append a space to your search pattern to get the desired results you're looking for. Now let's cycle through all these matches repeatedly by using N. So we'll keep hitting N and finally it wraps all the way back to the top. So we've visited every occurrence at least once. Now let's do the same thing in the reverse direction. So shift N, I'll keep hitting capital N until we're back and wrapped all the way around again, and I'll just hit it all the way back to the top of the file. Now let's search backward for the word to. To do that we'll type question mark T O and press enter. Now let's continue the reverse search until it wraps back around to the beginning with N. So I'll just keep hitting N until I see a message at the bottom of my screen that tells me that the search is wrapped. Now let's reverse the search with capital N until it wraps too. So there we go at wrapped as well. Now let's search for a word. Let's position our cursor under the first occurrence of the word it in the file and one way to do this is just to go to the beginning of the file with G G and then move to the second occurrence of the letter I, with two F I. Now type an asterisk and we move to the next appearance of the word it. Now we can use N to repeat our search until our cursor is back on the first line of the file. So we'll just keep hitting N. Now we're back to where we started. Let's practice searching for a word backwards. So let's position our cursor under the word us on the second line and one way to do that is just to move down a line with J and search backward with capital F lower case U. Now we'll just press the pound key which is shift three on my keyboard and this positions our cursor at the last occurrence of the word us in the file and we can keep repeating this search in this direction with N, so we'll keep hitting N, and then you see that you get a message at the bottom of the file that tells you that it wrapped. Let's keep going back until we're on the second line of the file. Finally, let's practice doing a global substitution. Now let's say we want to replace the word sat with laid and to do that, we can type colon percent sign, which is shift five on my keyboard, the substitute command, forward slash sat, which is the search pattern, laid, which is what we want to replace it with, forward slash G, which is the global flag, and we'll hit enter. Remember that the percent sign is a range which represents the entire file and also the G flag was used to make sure all occurrences of set were replaced with laid within the same line. If you're not sure if there are going to be multiple occurrences of your search pattern on a given line, then you should just go ahead and use the G flag to make sure that you do replace all occurrences. If you want to abandon your changes so you can try this practice exercise again, go ahead and type colon Q exclamation mark and press enter.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
3453
Courses
61
Learning Paths
8

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.