If you are trying to find a particular text, you can get vi to do it for you. You tell
If the search is successful, that is, the string is found, you are brought to that point in the text. If you decide that you want to search again, you have three choices. You can press ? or / and input the search string again; press n, which is the first letter of the word “next”; or simply press ? or / with no text following it for vi to continue the search in the applicable direction. If you wanted to find the next string that matches but in the opposite direction, what do you think the command would be? (Hint: the capital form of the “next” command.)
Once you have found what you are looking for, you can edit the text
all you want and then continue searching. This is because the search string you entered is kept in
a
You can also include movement commands in these searches. First, you enclose the search pattern with the character used to search (/ or ?), then add the movement command. For example, if you wanted to search backward for the phrase “hard disk” and then move up a line, you would enter ?hard disk?-. If you wanted to search forward for the phrase “operating system” and then move down three lines, you would enter /operating system/+3.
All this time, we have been referring to the text
patterns as search strings. As you just saw, you can actually enter phrases. In fact, you can use any
You can also do more complicated searches such as
No good text editor would be complete without the ability to not only search for text but to replace it as well. One way of doing this is to search for a pattern and then edit the text. Obviously, this starts to get annoying after the second or third instance of the pattern you want to replace. Instead, you could combine several of the tools you have learned so far.
For example, lets say that everywhere in the text you wanted to replace “Unix” with “UNIX.” First, do a search on
Unix with
Actually, this technique is good if you have a pattern that you want to replace, but not every time it appears. Instead, you want to replace the pattern selectively. You can just press n (or whatever) to continue the search without carrying out the replacement.
What if you know that you want to replace every instance of a pattern with something else? Are you destined to search and replace all 50 occurrences? Of course not. Silly you. There is another way.
Here I introduce what is referred to as escape or
An example of this would be:
This tells vi the scope is lines 45 through 100. The command is
Note that this only replaces the first occurrence on each line. To get all occurrences, we must include g for global at the end of each line, like this:
A problem arises if you want to modify only some of the occurrences. In this instance, you could add the modifier c for confirm. The command would then look like this:
This causes
If you wanted to search and replace on every line in the file, you could specify every line, such as
Once again, the developers of
Here again, the search patterns can be regular expressions. For example, if we wanted to replace every occurrence of “boat” (in either case) with the word “ship,” the command would look like this:
As with regular expressions in other cases, you can use the asterisk (*) to mean any number of the preceding characters or a period (.) to mean any single character. So, if you wanted to look for the word “boat” (again, in either case), but only when it was at the beginning of a line and only if it were preceded by at least one dash, the command would look like this:
The reason you have two dashes is that the search criteria specified at least one dash. Because the asterisk can be any number, including zero, you must consider the case where it would mean zero. That is, where the word “boat” was at the beginning of a line and there were no spaces. If you didn’t care what the character was as long as there was at least one, you could use the fact that in a search context, a dot means any single character. The command would look like this:
You can can simplify things by using the plus-sign (+) instead of the asterisk. The plus-sign will match 1 or more of the preceding character, whereas the asterisk matches the preceding character 0 or more times. This is a slight but important difference. If you use a question mark (?), it will match 0 or 1 times only. For example, if we had: