{"id":205,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:19","modified_gmt":"2020-08-22T20:26:19","slug":"this-is-the-page-title-toplevel-39","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=205","title":{"rendered":"Searching in vi"},"content":{"rendered":"\n<title>Searching in vi<\/title>\n<concept id=\"\" description=\"In 'vi', you use the question mark (?) to search backward.\" \/>\n<concept id=\"\" description=\"In 'vi', you use the forward (\/) to search forward.\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you use in 'vi' to search forward?\" \/>\n<question id=\"\" type=\"mc\" text=\"What key would you use in 'vi' to search backward?\" \/>\n<p>\nIf you are trying to find a particular text, you can get vi to do it for you. You tell <command>vi<\/command> that you want to enter a search pattern by pressing <keyinput>\/<\/keyinput> (slash). This will bring you down to the bottom line of the screen where you will see your slash. you then can type in what you want to look for. When you press Enter, vi will start searching from your current location down toward the bottom of the file. If you use press ? instead of \/, then vi will search from your string toward the top of the file.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"When searching in 'vi' which key moves to the next matching term?\" \/>\n<question id=\"\" type=\"mc\" text=\"When searching in 'vi' which key moves to the previous matching term?\" \/>\n<concept id=\"\" description=\"When searching in 'vi', the 'n' moves to the next match.\" \/>\n<concept id=\"\" description=\"When searching in 'vi', the 'N' moves to the previous match.\" \/>\n<p>\nIf the search is successful, that is, the string is found, you are brought to that point in the\ntext.\nIf you decide that you want to search again, you have three choices.\nYou can press ? or \/ and input the search string again; press n, which is the first letter of the word\n&#8220;next&#8221;; or simply press ? or \/ with no text following it for vi to\ncontinue 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 &#8220;next&#8221; command.)<\/p>\n<p>\nOnce you have found what you are looking for, you can edit the text\nall you want and then continue searching. This is because the search string you entered is kept in\na <glossary>buffer<\/glossary>.\nSo, when you press \/, ?, n, or N, the system remembers what you were looking for.\n<\/p>\n<concept id=\"\" description=\"In 'vi' you can combine searching with movement.\" \/>\n<question id=\"\" type=\"mc\" text=\"What would you input in 'vi' to search for 'Linux Tutorial' and then move down 7 lines?\" \/>\n<p>\nYou can also include movement commands in these searches. First, you enclose the search pattern\nwith the character used to search (\/ or ?), then add the movement command. For example,\nif you wanted to search backward for the phrase &#8220;hard disk&#8221; and then move up a line, you would\nenter ?hard disk?-. If you wanted to search forward for the phrase &#8220;operating system&#8221; and then move down three lines, you would enter \/operating system\/+3.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What would you input in 'vi' to search for 'Linux Tutorial' at the begining of the line?\" \/>\n<question id=\"\" type=\"mc\" text=\"What would you input in 'vi' to search for 'Linux Tutorial' at the end of the line?\" \/>\n<p>\nAll this time, we have been referring to the text\npatterns as search strings. As you just saw, you can actually enter phrases. In fact, you can use any\n<glossary>regular expression<\/glossary> you want when searching for patterns. For example, if you wanted\nto search for the pattern &#8220;Linux,&#8221; but only when it appears at the beginning of a line, you would\nenter \/^Linux. If you wanted to search for it at the end of the line, you\nwould enter \/Linux$.\n<\/p>\n<concept id=\"\" description=\"You can use regular expressions to search in 'vi'.\" \/>\n<p>\nYou can also do more complicated searches such as\n<keyinput>\/^new [Bb][Oo][Aa][Tt]<\/keyinput>, which will search for the word\n&#8220;new&#8221; at the beginning of a line, followed by the word &#8220;boat&#8221; with each letter in either case.\n<\/p>\n<p>\nNo good text\neditor would be complete without the ability to not only search for text but to replace it as well. One way of doing this\nis to search for a pattern and then edit the text. Obviously, this starts to get annoying after the second or third instance\nof the pattern you want to replace. Instead, you could combine several of the tools you have learned so far.\n<\/p>\n<p>\nFor example, lets say that everywhere in the text you wanted to replace &#8220;Unix&#8221; with &#8220;UNIX.&#8221; First, do a search on\nUnix with <keyinput>\/Unix<\/keyinput>, tell <command>vi<\/command> that you want to change that word with <keyinput>cw<\/keyinput>, then input UNIX. Now, search for the pattern again with \/, and simply press . (dot). (Remember that the dot command repeats your last command.) Now do the search and press the dot command again.\n<\/p>\n<p>\nActually, this technique is good if you have a pattern that you want to replace, but not every time it appears. Instead,\nyou want to replace the pattern selectively. You can just press n (or whatever) to continue the search without carrying\nout the replacement.\n<\/p>\n<p>\nWhat if you know that you want to replace every instance of a pattern with something else? Are you destined to\nsearch and replace all 50 occurrences? Of course not. Silly you. There is another way.\n<\/p>\n<question id=\"\" type=\"mc\" text=\"How do you get into 'ex-mode' or 'escape-mode' in 'vi'?\" \/>\n<concept id=\"\" description=\"To enter 'ex-mode' or 'escape-mode' in 'vi' you press the colon key (:).\" \/>\n<p>\nHere I introduce what is referred to as escape or <glossary>ex-mode<\/glossary>, because the commands you enter are the same as in\nthe ex editor. To get to ex-mode, press : (colon). As with searches, you are brought down to the bottom of the screen. This time you see the : (colon). The syntax is\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What is the general syntax for issuing commands in 'vi'?\" \/>\n<concept id=\"\" description=\"The general syntax for issue commands in 'vi' is : &lt;scope&gt; &lt;command&gt;\" \/>\n<p>\n<keyinput>: &lt;scope&gt; &lt;command&gt;<\/keyinput>\n<\/p>\n<p>\nAn example of this would be:\n<\/p>\n<p>\n<commandexample>:45,100s\/Unix\/UNIX\/<\/commandexample>\n<\/p>\n<p>\nThis tells vi the scope is lines 45 <em>through <\/em>100. The command is <keyinput>s\/Unix\/UNIX\/<\/keyinput>, which says you want to\nsubstitute (s) the first pattern (Unix) with the second pattern (UNIX). Normally in English, we would say &#8220;substitute\n<glossary>UNIX<\/glossary>\nfor Unix.&#8221; However, the order here is in keeping with the UNIX pattern of source first, then destination (or, what it was is\nfirst, and what it will become is second, like mv source destination).\n<\/p>\n<p>\nNote that this only replaces the first occurrence on each line. To get all occurrences, we must include g for\nglobal at the end of each line, like this:<\/p>\n<p>\n<keyinput>:45,100s\/Unix\/UNIX\/g <\/keyinput>\n<\/p>\n<p>\nA problem arises if you want to modify only some of the occurrences. In this instance, you could add the\nmodifier c for confirm. The command would then look like this:\n<\/p>\n<p>\n<keyinput>:45,100s\/Unix\/UNIX\/gc <\/keyinput>\n<\/p>\n<p>\nThis causes <command>vi<\/command> to ask for confirmation before it makes the change.\n<\/p>\n<p>\nIf you wanted to search and replace on every line in the file, you could specify every line, such as <keyinput>:1,48<\/keyinput>.,\nassuming there were 48 lines in the file. (By the way, use <keyinput>Ctrl-g<\/keyinput>\nto find out what line you are on and how many\nlines there are in the file.) Instead of checking how many lines there are each time, you can simply use the\nspecial character <keyinput>$<\/keyinput> to indicate the end of the file. (Yes, <keyinput>$<\/keyinput> also means the end of the line, but in this context, it means the end of the file.) So, the scope of the command would look like <keyinput>:1,$<\/keyinput>.\n<\/p>\n<p>\nOnce again, the developers of <command>vi<\/command> made life easy for you. They realized that making changes throughout a\nfile is something that is probably done a lot. They included a special character to mean the entire file: {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}. Therefore, the command is written as {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63} = 1,$.\n<\/p>\n<p>\nHere again, the search patterns can be regular expressions. For example, if we wanted to replace every occurrence of &#8220;boat&#8221; (in either case) with the word &#8220;ship,&#8221; the command would look like this:\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/[Bb][Oo][Aa][Tt]\/ship\/g <\/keyinput>\n<\/p>\n<p>\nAs with regular expressions in other cases, you can use the asterisk (*) to mean any number of the\npreceding characters or a period (.) to mean any single character. So, if you wanted to look for the word\n&#8220;boat&#8221; (again, in either case), but only when it was at the beginning of a line and only if it were preceded\nby at least one dash, the command would look like this:\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/^&#8211;*[Bb][Oo][Aa][Tt]\/ship\/g<\/keyinput>\n<\/p>\n<p>\nThe reason you have two dashes is that the search criteria specified\n<em>at least<\/em> one dash.\nBecause the asterisk can be <em>any<\/em> number, including zero, you must consider the case where\nit would mean zero. That is, where the word &#8220;boat&#8221; was at the beginning of a line and there were no spaces.\nIf you didn&#8217;t care what the character was as long as there was at least one, you could use the fact that in a\nsearch context, a dot means any single character. The command would look like this:\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/^..*[Bb][Oo][Aa][Tt]\/ship\/g<\/keyinput>\n<\/p>\n<p>\nYou 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:\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/^-?[Bb][Oo][Aa][Tt]\/ship\/g<\/keyinput>\n<\/p>\nWe would only match if there was 0 or 1 dash at the start of the line. If there were two or more, it would not match.\n","protected":false},"excerpt":{"rendered":"<p>Searching in vi If you are trying to find a particular text, you can get vi to do it for you. You tell vi that you want to enter a search pattern by pressing \/ (slash). This will bring you &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=205\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-205","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=205"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/205\/revisions"}],"predecessor-version":[{"id":689,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/205\/revisions\/689"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}