{"id":206,"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:41","modified_gmt":"2020-08-22T20:26:41","slug":"this-is-the-page-title-toplevel-40","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=206","title":{"rendered":"vi Buffers"},"content":{"rendered":"\n<title>vi Buffers<\/title>\n<p>\nRemember when we first starting talking about searching, I mentioned that the expression you were looking for was held in\na <glossary>buffer<\/glossary>. Also, whatever was matched by <keyinput>\/[Bb][Oo][Aa][Tt]<\/keyinput> can be held in a buffer.\nWe can then use that buffer as part of the replacement expression. For example, if we wanted to replace\nevery occurrence of &#8220;UNIX&#8221; with &#8220;Linux,&#8221; we could do it like this:\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/UNIX\/Linux\/g<\/keyinput>\n<\/p>\n<concept id=\"\" description=\"When specifying a 'scope' in vi command-mode, the percent -sign ({3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}) refers to the entire file.\" \/>\n<question id=\"\" type=\"mc\" text=\"When specifying a 'scope' in vi command-mode, what character refers to the entire file?\" \/>\n<concept id=\"\" description=\"To enclose something in a search buffer in 'vi', you enclose it within a matching pair of back slashes and parenthesis: \\( \\)\" \/>\n<p>\nThe scope of this command is defined by the <keyinput>{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}<\/keyinput>, the shortcut way of referring to the entire text.\nOr, you could first save &#8220;UNIX&#8221; into a <glossary>buffer<\/glossary>, then use it in the replacement\nexpression. To enclose something in a buffer, we enclose it within a matching pair of back slashes\n<keyinput>\\(<\/keyinput> and <keyinput>\\)<\/keyinput>\nto define the extent of a buffer. You can even have multiple pairs that define the extent of  multiple buffers.\nThese are reference by <keyinput>\\#<\/keyinput>, where <keyinput>#<\/keyinput> is the number of the buffer.\n<\/p>\n<p>\nIn this example\n<\/p>\n<p>\n<keyinput>:{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/\\(UNIX\\)\/Linux \\1\/g <\/keyinput>\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What 'vi' substitution would replace every occurance of 'Windows' with 'Linux'?\" \/>\n<p>\nthe text &#8220;UNIX,&#8221; is placed into the first <glossary>buffer<\/glossary>.\nYou then reference this buffer with <keyinput>\\1 <\/keyinput>to tell\n<command>vi<\/command> to plug in the contents of the first buffer.\nBecause the entire search pattern is the same as the pattern buffer, you could also have written it like this\n<\/p>\n<p>\n<keyinput> :{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/\\(UNIX\\)\/Linux &amp;\/g <\/keyinput>\n<\/p>\n<p>\nin which the ampersand represents the entire search pattern.\n<\/p>\n<p>\nThis obviously doesn&#8217;t save much typing. In fact, in this example, it requires more typing to save &#8220;UNIX&#8221; into the <glossary>buffer<\/glossary>\nand then use it. However, if what you wanted to save was longer, you would save time. You also save time\nif you want to use the buffer twice. For example, assume you have a file with a list of other files, some of them\nC language source files. All of them end in .c. You now want to change just the  names of the C files so that\nthe ending is &#8220;old&#8221; instead of .c. To do this, insert mv at the beginning of each line as well as produce two\ncopies of the file name: one with .c and one with .old. You could do it like this:\n<\/p>\n<p>\n<keyinput> :{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/^\\(.*\\)\\.c\/mv \\1.c \\1.old\/g <\/keyinput>\n<\/p>\n<p>\nIn English, this line says:\n<\/p>\n<ul>\n<li>For every line ({3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63})<\/li>\n<li>substitute (s)<\/li>\n<li>for the pattern starting at the beginning of the line (^), consisting of any number of characters ( \\(.*\\) ) (placing this pattern into <glossary>buffer<\/glossary> #1) followed by .c<\/li>\n<li>and use the pattern mv, followed by the contents of <glossary>buffer<\/glossary>\n#1 (\\1), followed by a .c, which is again followed by the contents of buffer #1, (\\1) followed by .old<\/li>\n<li>and do this for every line (g), (i.e., globally)<\/li><\/ul>\n<p>\nNow each line is of the form\n<\/p>\n<p>\n<screenoutput>mv file.c file.old <\/screenoutput>\n<\/p>\n<p>\nNote the slash preceeding the dot in the expression &#8220;\\.c&#8221;. The slash &#8220;protects&#8221; the dot from\nbeing interpreted as the metacharacter for &#8220;any character&#8221;. Instead, you want to search\nfor a literal dot, so you need to protect it.\n<p>\nWe can now change the <glossary>permissions<\/glossary>\nto make this a shell script and execute it. We would then move all the files as described above.\n<\/p>\n<p>\nUsing numbers like this is useful if there is more that one search pattern that you want to process.\nFor example, assume that we have a three-column table for which we want to change the order of the\ncolumns. For simplicity&#8217;s sake, lets also assume that each column is separated by a space so as not to\nmake the search pattern too complicated.\n<\/p>\n<p>\nBefore we start, we need to introduce a new concept to vi, but one that you have seen before: [ ]. Like the\n<glossary>shell<\/glossary>,  the square bracket pair ([ ]) of vi is used to limit sets of characters.\nInside of the brackets, the <glossary>caret<\/glossary> (^) takes on a new meaning. Rather than\nindicating the beginning of a line, here it negates the character we are searching for. So we could type\n<\/p>\n<p>\n<keyinput>{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/\\([^  ]*\\)\\([^  ]*\\)\\([^  ]*\\)\/\\3 \\1 \\2\/g<\/keyinput>\n<\/p>\n<p>\nHere we have three regular expressions, all referring to the same thing: \\([^ ]*\\). As we discussed above,\nthe slash pair \\( and \\) delimits each of the buffers, so everything inside is the search pattern. Here, we are\nsearching for [^ ]*, which is any number of matches to the set enclosed within the brackets. Because the\nbrackets limit a set, the set is ^, followed by a space. Because the ^ indicates negation, we are placing any\nnumber of characters that is <i>not<\/i> a space into the <glossary>buffer<\/glossary>.  In the replacement\npattern, we are telling vi to print pattern3, a space, pattern1, another space, then pattern2.\n<\/p>\n<p>\nIn the first two instances, we followed the pattern with a space. As a result, those spaces were not saved into\nany of the buffers. We did this because we may have wanted to define our column separator differently.\nHere we just  used another space.\n<\/p>\n<p>\nI have often had occasion to want to use the pattern buffers more than once. Because they are not\ncleared after each use, you can use them as many times as you want. Using the example above, if we change\nit to\n<\/p>\n<p>\n<keyinput>{3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}s\/\\([^  ]*\\)\\([^  ]*\\) >\\([^  ]*\\)\/\\3  \\1  \\2  \\1\/g<\/keyinput>\n<\/p>\n<p>\nwe would get pattern3, then pattern1, then pattern2, and at the end, pattern1 again.\n<\/p>\n<p>\nBelieve it or not, there are still more buffers. In fact, there are dozens that we haven&#8217;t touched on. The\nfirst set is the numbered buffers, which are numbered 1-9. These are used when we delete\ntext and they behave like a <glossary>stack<\/glossary>. That is, the first time we delete something, say a\nword, it is placed in <glossary>buffer<\/glossary> number 1. We next delete a line that is placed in\nbuffer 1 and the word that was in buffer 1 is placed in buffer 2. Once all the numbered buffers all full,\nany new deletions push the oldest ones out the bottom of the stack and are no longer available.\n<\/p>\n<p>\nTo access these buffers, we first tell vi that we want to use one of the buffers by pressing the double-quote (&#8220;).\nNext, we specify then the number of the <glossary>buffer<\/glossary>, say 6, then we type either <keyinput>p<\/keyinput> or <keyinput>P<\/keyinput> to put it, as in <keyinput>&#8220;6p<\/keyinput>. When you delete text and then do a put without specifying any buffer, it automatically comes from buffer 1.\n<\/p>\n<concept id=\"\" description=\"You can use 26 named buffers in 'vi'.\" \/>\n<question id=\"\" type=\"mc\" text=\"How many named buffers does 'vi' have.\" \/>\n<question id=\"\" type=\"mc\" text=\"What keys would you press in 'vi' put the next 10 lines into the 'f' named buffer?\" \/>\n<p>\nThere are some other buffers, in fact, 26 of them, that you can use by name. These are the named\nbuffers. If you can&#8217;t figure out what their names are, think about how many of them there are (26). With these\nbuffers, we can intentionally and specifically place something into a particular buffer. First, we say\nwhich <glossary>buffer<\/glossary> we want by preceding its name with a double-quote (&#8220;); for example,\n&#8220;f. This says that we want to place some text in the named buffer f. Then, we place the data in the buffer, for example, by deleting an entire line with <keyinput>dd<\/keyinput> or by deleting two words with d2w. We can\nlater put the contents of that  buffer with <keyinput>&#8220;fp<\/keyinput>. Until we place something new in that buffer, it will contain what\nwe originally deleted.\n<\/p>\n<p>\nIf you want to put something into a <glossary>buffer<\/glossary>\nwithout having to delete it, you can. You do this by &#8220;yanking it.&#8221; To yank an entire line, you could done one\nof several things. First, there is <keyinput>yy<\/keyinput>. Next, Y. Then, you could use y, followed by a movement commands,\nas in y-4, which would yank the next four lines (including the current one), or y\/expression, which would\nyank everything from your current position up to and including expression.\n<\/p>\n<p>\nTo place yanked data into a named <glossary>buffer<\/glossary>\n(rather than the default buffer, buffer number 1), it is the same procedure as when you delete. For example, to\nyank the next 12 lines into named buffer h, we would do &#8220;h12yy. Now those 12 lines are available to us.\nKeep in mind that we do not have to store full lines. Inputting &#8220;h12yw will put the next 12 words into\nbuffer h.\n<\/p>\n<p>\nSome of the more observant readers might have noticed that because there are 26 letters and each has both an upper- and lowercase, we could have 52 named buffers. Well, up to now, the uppercase letters did something different. If uppercase letters were used to designate different buffers, then the pattern would be compromised. Have no fear, it is.\n<\/p>\n<p>\nInstead of being different buffers than their lowercase brethren, the uppercase letters are the <em>same<\/em> <glossary>buffer<\/glossary>. The difference is that yanking or deleting something into an uppercase buffer appends the contents rather\n that overwriting them.\n<\/p>\n<p>\nYou can also have vi keep track of up to 26 different places with the file you are editing. These functions are just like bookmarks in word processors. (Pop quiz: If there 26 of them, what are their names?)\n<\/p>\n<concept id=\"\" description=\"You can 'bookmark' a place in a file in 'vi' by using the keys m' (m single-quote ) followed by a single letter.\" \/>\n<concept id=\"\" description=\"You can return to 'bookmark' a place in a file in 'vi' by using the keys m' (m single-quote ) followed by the letter for the mark (i.e. m'h).\" \/>\n<question id=\"\" type=\"mc\" text=\"What key combination would set thje bookmark m in 'vi'?\" \/>\n<p>\nTo mark a spot, move to that place in the file, type m for mark (what else?), then a single back\nquote (`), followed by the letter you want to use for this bookmark. To go back to that spot, press the\nback quote (`), followed by the appropriate letter. So, to assign a bookmark q to a particular spot, you\nwould enter <keyinput>`q<\/keyinput>. Keep in mind that reloading the current file or editing a new one makes you lose the bookmarks.\n<\/p>\nNote that with newer version of <command>vi<\/command> (particularly <command>vim<\/command>) you don&#8217;t press the backquote to set the mark, just m followed by the appropriate letter.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>vi Buffers Remember when we first starting talking about searching, I mentioned that the expression you were looking for was held in a buffer. Also, whatever was matched by \/[Bb][Oo][Aa][Tt] can be held in a buffer. We can then use &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=206\">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-206","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/206","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=206"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/206\/revisions"}],"predecessor-version":[{"id":780,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/206\/revisions\/780"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=206"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}