{"id":207,"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-41","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=207","title":{"rendered":"vi Magic"},"content":{"rendered":"\n<title>vi Magic<\/title>\n<p>\nI imagine that long before now, you have wondered how to turn on all that\nmagic I said that <command>vi<\/command> could do. Okay, let&#8217;s do it.\n<\/p>\n<p>\nThe first thing I want to talk about is abbreviations. You can tell vi that\nwhen you type in a specific set of characters it is supposed to automagically\nchange it to something else. For example, we could have <command>vi<\/command> always change &#8220;USA&#8221; to &#8220;United States of America&#8221;. This is done with the <filecontents type=\"\">abbr<\/filecontents> command.\n<\/p>\n<concept id=\"\" description=\"You can configure 'vi' to automatically change words as you type.\" \/>\n<question id=\"\" type=\"mc\" text=\"How would you create an abbreviation in 'vi' to always change 'CIA' to 'Central Intelligence Agency'?\" \/>\n<p>\nTo create a new abbreviation, you must get into ex-mode by pressing the\ncolon (:) in command mode. Next, type in abbr, followed by what you want to type\nin, and what vi should change it to. For example:\n<\/p>\n<p>\n<keyinput>:abbr USA United States of America<\/keyinput>\n<\/p>\n<p>\nNote that the abbreviation cannot contain any spaces because vi interprets\neverything after the second word as being part of the expansion.\n<\/p>\n<p>\nIf we later decide we don&#8217;t want that abbreviation anymore, we enter\n<\/p>\n<p>\n<keyinput>:unabbr USA<\/keyinput>\n<\/p>\n<p>\nBecause it is likely that we will want to use the abbreviation USA, it is\nnot a good idea to use an abbreviation that would normally occur, such as USA.\nIt would be better, instead, to use an abbreviation that doesn&#8217;t occur normally,\nlike Usa. Keep in mind, that abbreviations only apply to complete words.\nTherefore, something like the name &#8220;Sousa&#8221; won&#8217;t be translated to &#8220;SoUSA.&#8221; In\naddition, when your abbreviation is followed by a space, <keyinput>Tab<\/keyinput>, <keyinput>Enter<\/keyinput>, or <keyinput>Esc<\/keyinput>, the\nchange is made.\n<\/p>\n<p>\nLets take this one step further. What if we were always spelling &#8220;the&#8221; as\n&#8220;teh.&#8221; We could then create an abbreviation\n<\/p>\n<p>\n<keyinput>:abbr teh the<\/keyinput>\n<\/p>\n<concept id=\"\" description=\"With 'vi' you can configure a rudimentary spell checker.\" \/>\n<question id=\"\" type=\"tf\" text=\"The is no way in 'vi' to automatically correct frequently mispelled words.\" \/>\n<p>\nEvery time we misspell &#8220;the&#8221; as &#8220;teh,&#8221; vi would automatically correct it. If\nwe had a whole list of words that we regularly misspelled and created similar\nabbreviations, then every time we entered one of these misspelled words, it\nwould be replaced with the correctly spelled word. Wouldn&#8217;t that be automatic\nspell correction?\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What key combination would you press in 'vi' ex-mode to ingore the meaning of special characters?\" \/>\n<concept id=\"\" description=\"In 'vi' ex-mode you use Ctrl-V to ingore the meaning of special characters.\" \/>\n<p>\nIf we ever want to &#8220;force&#8221; the spelling to be a particular way (that is,\nturn off the abbreviation momentarily), we simply follow the abbreviation with a\n<keyinput>Ctrl-V<\/keyinput>. This tells <command>vi<\/command> to ignore the special meaning of the following character.\nBecause the next character is a white space, which would force the expansion of\nthe abbreviation (which makes the white space special in this case), &#8220;turning\noff&#8221; the white space keeps the abbreviation from being expanded.\n<\/p>\n<concept id=\"\" description=\"You can also use vi to re-map certain key sequences.\" \/>\n<p>\nWe can also use vi to re-map certain sequences. For example, I have created\na command so that all I need to do to save a file is <keyinput>Ctrl-W <\/keyinput>(for <u>w<\/u>rite).\nIf I want to save the file and quit, I enter <keyinput>Ctrl-X<\/keyinput> with the &#8220;map&#8221; command.\n<\/p>\n<p>\nThe most common maps that I have seen have used control sequences, because\nmost of the other characters are already taken up. Therefore, we need to\nside-step a moment. First, we need to know how to\naccess <glossary term=\"control character\">control characters<\/glossary> from within vi. This is done in either command\nmode or input mode by first pressing <keyinput>Ctrl-V <\/keyinput>and then pressing the\n<glossary>control character<\/glossary> we want. So to get <keyinput>Ctrl-W<\/keyinput>, you would type\n<keyinput>Ctrl-V<\/keyinput>, then <keyinput>Ctrl-W<\/keyinput>. This would appear on the screen as ^W. This looks like two\ncharacters, but if you inserted it into a text file and moved\nover it with the <glossary>cursor<\/glossary>,  you would realize that vi sees it\nas only one character. Note that although I pressed the lowercase w, it will\nappear as uppercase on the screen.\n<\/p>\n<p>\nSo, to map <keyinput>Ctrl-W<\/keyinput> so that every time we press it, we write our current file to disk, the command would be\n<\/p>\n<p>\n<keyinput>map ^W :w^M<\/keyinput>\n<\/p>\n<p>\nThis means that when we press <keyinput>Ctrl-W,<\/keyinput> vi interprets it as though we actually\ntyped <keyinput>:w<\/keyinput> and pressed Enter (the <keyinput>Ctrl-M<\/keyinput>: <keyinput>^M<\/keyinput>). The enter key at the end of the command\nis a good idea because you usually want the command to be executed right away.\nOtherwise, you would have to press Enter yourself.\n<\/p>\n<question id=\"\" type=\"tf\" text=\"In 'vi', you can map only control character and not function keys.\" \/>\n<concept id=\"\" description=\"In 'vi', you can map not only control character, but  function keys, as well.\" \/>\n<p>\nAlso keep in mind that this can be used with the function keys. Because I am\naccustomed to many Windows applications in which the F2 key means to save, I map F2 to <keyinput>Ctrl-V<\/keyinput>, then <keyinput>F2<\/keyinput>. It looks like this:\n<\/p>\n<p>\n<keyinput>map ^[[N :w^M<\/keyinput> (The ^[[N is what the F2 key displays on the screen)\n<\/p>\n<p>\nIf we want, we can also use shifted function characters. Therefore, we can\nmap <keyinput>Shift-F2<\/keyinput> to something else. Or, for that matter, we can also use shifted and control function keys.\n<\/p>\n<p>\nIt has been my experience that, for the most part, if you use <keyinput>Shift<\/keyinput> and <keyinput>Ctrl<\/keyinput>\nwith non-function keys, vi only sees <keyinput>Ctrl<\/keyinput> and not <keyinput>Shift<\/keyinput>. Also, <keyinput>Alt<\/keyinput> may not work\nbecause on the system console, <keyinput>Alt<\/keyinput> plus a function key tells the system to switch to multiscreens.\n<\/p>\n<p>\nI try not to use the same key sequences that <command>vi<\/command> already does. First, it\nconfuses me because I often forget that I remapped something. Second, the real\nvi commands are then inaccessible. However, if you are used to a different\ncommand set (that is, from a different editor), you can &#8220;program&#8221; vi to behave\nlike that other editor.\n<\/p>\n<p>\nNever define a mapping that contains its own name, as this ends up\nrecursively expanding the abbreviation. The classic example is :map! n\nbanana. Every time you typed in the word &#8220;banana,&#8221; you&#8217;d get\n<\/p>\n<p>\n<screenoutput>bababababababababababababababababa&#8230;<\/screenoutput>\n<\/p>\n<p>\nand depending on what version you were running, vi would catch the fact that\nthis is an infinite translation and stop.\n","protected":false},"excerpt":{"rendered":"<p>vi Magic I imagine that long before now, you have wondered how to turn on all that magic I said that vi could do. Okay, let&#8217;s do it. The first thing I want to talk about is abbreviations. You can &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=207\">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-207","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/207","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=207"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/207\/revisions"}],"predecessor-version":[{"id":781,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/207\/revisions\/781"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}