{"id":202,"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-36","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=202","title":{"rendered":"vi Basics"},"content":{"rendered":"\n<title>vi Basics<\/title>\n<p>\nThe uses and benefits of any editor like vi are almost religious. Often, the\nreasons people choose one editor over another are purely a matter of personal\ntaste. Each offers its own advantages and functionality. Some versions of UNIX\nprovide other editors, such as emacs. However, the nice thing about vi is that\nevery dialect of <glossary>UNIX<\/glossary>\nhas it. You can sit down at any <glossary>UNIX<\/glossary>\nsystem and edit a file. For this reason more than any other, I think it is  worth learning.\n<\/p>\n<p>\nOne problem <command>vi<\/command> has is that can be very intimidating. I know, I didn&#8217;t\nlike it at first. I frequently\nget into discussions with people who have spent less than 10 minutes using it\nand then have ranted about how terrible it was. Often, I then saw them spending\nhours trying to find a free or relatively cheap add-on so they didn&#8217;t have to\nlearn vi. The problem with that approach is that if they has spent as much time learning\nvi as they did trying to find an alternative, they actually could have become quite proficient with\nvi.\n<\/p>\n<p>\nThere is more to <command>vi<\/command> than just its availability on different <glossary>UNIX<\/glossary>\nsystems. To\nme, vi is magic. Once you get over the initial intimidation, you will see that\nthere is a logical order to the way the commands are laid out and fit together.\nThings fit together in a pattern that is easy to remember. So, as we get into\nit, let me tempt you a little.\n<\/p>\n<p>\nAmong the &#8220;magical&#8221; things vi can do:\n<\/p>\n<ul>\n<li>Automatically correct words that you misspell often <\/li>\n<li>Accept user-created vi commands <\/li>\n<li>Insert the output of <glossary>UNIX<\/glossary>\ncommands into the file you are editing<\/li>\n<li>Automatically indent each line <\/li>\n<li>Shift sets of lines left or right<\/li>\n<li>Check for pairs of {}, () and [] (great for programmers)<\/li>\n<li>Automatically wrap around at the end of a line <\/li>\n<li>Cut and paste between documents <\/li>\n<\/ul>\n<p>\nI am not going to mention every single vi command. Instead, I am going to\nshow you a few and how they fit together. At the end of this section, there is a\ntable containing the various commands you can use inside vi. You can then apply\nthe relationships to the commands I don&#8217;t mention.\n<\/p>\n<p>\nTo see what is happening when you enter commands, first find a file that you\ncan poke around in. Make a copy of the termcap file (\/etc\/termcap) in a\ntemporary directory and then edit it (cd \/tmp; cp \/etc\/termcap . ; vi termcap).\nThe termcap file contains a list of the capabilities of various terminals. It is\nusually quite large and gives you a lot of things to play with in vi.\n<\/p>\n<question id=\"101\" text=\"What are the two modes in vi?\" \/>\n<p>\nBefore we can jump into the more advanced features of vi, I need to  cover\nsome of the basics. Not command basics, but rather some behavioral basics. In\nvi, there are two modes: command mode and input mode. While you are in command\nmode, every keystroke is considered part of a command. This is where you\nnormally start when you first invoke vi. The reverse is also true. While in\ninput mode, everything is considered input.\n<\/p>\n<p>\nWell, that isn&#8217;t entirely true and we&#8217;ll talk about that in a minute.\nHowever, just remember that there are these two modes. If you are in command\nmode, you go into input mode using a command to get you there, such as append or\ninsert (I&#8217;ll talk about these in a moment). If you want to go from input mode to\ncommand mode, press Esc.\n<\/p>\n<p>\nWhen vi starts, it goes into full-screen mode (assuming your <glossary>terminal<\/glossary>\nis set\nup correctly) and it essentially clears the screen (see the following image). If we start the command as\n<p>\n<commandexample>vi search<\/commandexample>\n<\/p>\n<p>\nat the bottom of the screen, you see\n<\/p>\n<screenoutput>&#8220;search&#8221; [New File]<\/screenoutput>\n<\/p>\nYour <glossary>cursor<\/glossary>\nis at the top left-hand corner of the screen, and there is a column\nof tildes (~) down the left side to indicate that these lines are nonexistent.\n<\/p>\n<p>  In the image below we see a vi session started from a terminal window running under X-Windows. This\nis essentially the same thing you will see when starting vi from any command line.\n<p>\n<img decoding=\"async\" src=\"start_vi.png\" width=588 height=399 border=0  usemap=\"#start_vi_map\">\n<map name=\"start_vi_map\">\n<!-- #$-:Image Map file created by GIMP Imagemap Plugin -->\n<!-- #$-:GIMP Imagemap Plugin by Maurits Rijk -->\n<!-- #$-:Please do not edit lines starting with \"#$\" -->\n<!-- #$VERSION:1.3 -->\n<!-- #$AUTHOR:James Mohr -->\n<area shape=\"RECT\" coords=\"8,343,564,362\" href=\"popup#vi#command and status area\">\n<area shape=\"RECT\" coords=\"5,50,562,342\" href=\"popup#vi#Text area\">\n<p>\n<icaption>Image &#8211; Main vi window. (<b>interactive<\/b>)<\/icaption>\n<p>\nAs with most text editors or word processors, <command>vi<\/command> gives you the ability to save the file you are editing\nwithout stopping the program. To issue the necessary command we first input a colon (:) when in command\nmode.  When then press <keyinput>w<\/keyinput> (for write) and the press the enter key. This might look like the following figure:\n<p>\n<img decoding=\"async\" src=\"save_vi.png\" width=578 height=80 border=0>\n<\/p>\n<p>\n<icaption>Image &#8211; Writing a file in vi.<\/icaption>\n<p>\nAfter you press the enter key, you end up with something like the following image:\n<p>\n<img decoding=\"async\" src=\"save_vi2.png\" width=614 height=90 border=0 usemap=\"#save_vi2_map\">\n<map name=\"save_vi2_map\">\n<!-- #$-:Image Map file created by GIMP Imagemap Plugin -->\n<!-- #$-:GIMP Imagemap Plugin by Maurits Rijk -->\n<!-- #$-:Please do not edit lines starting with \"#$\" -->\n<!-- #$VERSION:1.3 -->\n<!-- #$AUTHOR:James Mohr -->\n<area shape=\"RECT\" coords=\"0,36,59,58\" href=\"popup#vi#Name of file being saved.\">\n<area shape=\"RECT\" coords=\"56,38,91,56\" href=\"popup#vi#Number of lines in the current file.\">\n<area shape=\"RECT\" coords=\"90,36,123,57\" href=\"popup#vi#Number of characters in the current file.\">\n<area shape=\"RECT\" coords=\"451,30,529,55\" href=\"popup#vi#Current position in the file\">\n<\/map>\n<icaption>Image &#8211; Writing a file in vi. (<b>interactive<\/b>)<\/icaption>\n<question id=\"485\" text=\"What key combination is use to 'force' vi to write a file?\" \/>\n<p>\nIf you are editing a file that already existing and try to save it like this, you may get an error message\nthat says the file is read only. You will also get this message, when trying to save a file from &#8220;view&#8221;,\nwhich is the &#8220;read-only&#8221; version of vi. To force the file to be written, you follow the w with an exclamation\nmark: <keyinput>:w!<\/keyinput> .\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What is 'ex-mode' in 'vi'?\" \/>\n<p>\nThe <glossary>ex-mode<\/glossary> (or command mode) also allows you to do many other things with the file itself. Among them are\n<\/p>\n<ul>\n<li>:q to quit the file (:q! if the file has been changed and you don&#8217;t want to save the changes) <\/li>\n<li>:wq to write the file and quit <\/li>\n<li>:e to edit a new file (or even the same file) <\/li>\n<li>:r to read in a new file starting at the current location <\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>vi Basics The uses and benefits of any editor like vi are almost religious. Often, the reasons people choose one editor over another are purely a matter of personal taste. Each offers its own advantages and functionality. Some versions of &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=202\">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-202","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/202","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=202"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/202\/revisions"}],"predecessor-version":[{"id":779,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/202\/revisions\/779"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}