Search, Find, and Replace

#### Goal:

The goal of this exercise is to practice searching, finding, and replacing text within a file.

#### Instructions:

#### Open the search-practice.txt file

First, start a command line session on your local machine. Next, use vim to open the "search-practice.txt" file that came in the course downloads. To do that, navigate to the location of the file. Remember this could be different for you depending on where you extracted the contents of the file. This example assumes the course download was saved into your Downloads folder and extracted from there.

cd Downloads cd vimclass vim search-practice.txt

#### Search within the same line

Here is the first line in the file:

The Time Traveller (for so it will be convenient to speak of him) was expounding

Position your cursor at the beginning of the word "for" on first line in the file. To do that type ff. Now, place your cursor under the "f" in the word "of". This is simply repeating the ff command. Remember, to repeat your linewise search use a semicolon: ;.

Now, place your cursor back at the beginning of the word "for". To do that, repeat your search in the opposite direction with the comma: ,.

Now, place your cursor at the beginning of the word "Traveller". To do that, search backwards with FT.

Position your cursor just one character before the word "be", so that your cursor is on the space. To do that use the till command: tb.

Position your cursor at the space before the word "speak." To do that type: ts.

Position your cursor on the space after the word "for". Type: ###b/b###.

#### Search within the file

Find all occurrences of the word "and" and position your cursor at the beginning of each occurrence at least once. To do this, type /and<SPACE><ENTER>. (Note: if you did not append a space to the word and you would have matched incandescent.) Now cycle through all the matches by repeatedly pressing n.

Now reverse the direction of your search and position your cursor at the beginning of each of the occurrences by repeatedly pressing N.

Now search backward for the word "to". To do that type ?to<ENTER>. Continue your reverse search until it wraps back to the beginning with n. Now reverse the reverse search with N, until it wraps.

#### Search for a word

Position your cursor under the first occurrence of the word "it" in the file. One way to do this is to go the beginning of the with with gg and then move to the second occurrence of the letter "i" with 2fi. Now type * to move to the next appearance of the word "it". Use n to repeat your search until your cursor is back on the first line of the file.

Let's practice searching for a word backward. Position your cursor under the word "us" on the second line. One way to position your cursor there is to move down a line with j and search backwards with Fu. Now press the # key. This positions your cursor at the last occurrence of the word "us" in the file. Repeat your search with n. Finally press n again to position your cursor back to the word "us" on the second line in the file.

#### Perform a global substitution

Replace all the occurrences of the word "sat" with "laid". To do that type :%s/sat/laid/g<ENTER>. Remember that % is a range which represents the entire file. Also, the g flag was used to make sure all occurrences of "sat" was replaced with "laid" within the same line. If you're not sure if there are going to multiple occurrences of your search pattern in a line, then you should use the g flag to be sure you replace all occurrences.

#### Exit out of vim

If you want to abandon your changes so you can try this practice exercise again, use :q!<ENTER>.