{"id":266,"date":"2014-05-02T19:56:00","date_gmt":"2014-05-02T19:56:00","guid":{"rendered":"https:\/\/questy.org\/?p=266"},"modified":"2024-09-26T19:07:59","modified_gmt":"2024-09-26T19:07:59","slug":"vim","status":"publish","type":"post","link":"https:\/\/questy.org\/index.php\/2014\/05\/02\/vim\/","title":{"rendered":"Vim"},"content":{"rendered":"\n<p><strong>Old School<\/strong><\/p>\n\n\n\n<p>Why on earth am I starting with Vim? &nbsp;(or \u201cvi\u201d for you old folks)<\/p>\n\n\n\n<p>Vim is the modern \u201cvi\u201d implementation. &nbsp;A full-screen text editor with a myriad of options and abilities for beyond anything I could ever cover here. &nbsp;But Vim has one thing going for it that no other text editor has. &nbsp;One simple fact about it puts it in the category of cameras. &nbsp;You know the old saying?<\/p>\n\n\n\n<p><em><strong>\u201cThe best camera is the one you have with you.\u201d<\/strong><\/em><\/p>\n\n\n\n<p>Thus it is with vi\/vim. &nbsp;It\u2019s literally&nbsp;<strong>everywhere.<\/strong><\/p>\n\n\n\n<p>Every UNIX OS, commercial or not, streamlined or not, old or new, has vi or vim installed on it. &nbsp;Emacs is a great product, but it just isn\u2019t installed by default everywhere. &nbsp;Regardless of the editor you refer to, ${INSERT_EDITOR_HERE} just isn\u2019t as ubiquitous as Vi\/Vim.<\/p>\n\n\n\n<p>Since we\u2019re talking about a modern pursuit and workflow (DEVOPS), we\u2019ll be talking mostly about Vim\u2019s capabilities and features.<\/p>\n\n\n\n<p><strong>What Vim is&nbsp;<em>NOT<\/em><\/strong><\/p>\n\n\n\n<p>Vim is not a word processor. &nbsp;You won\u2019t be writing business letters with it. &nbsp;Vim is not for writing resumes, making pretty newsletters, or for typesetting a magazine. &nbsp;The die-hard Vi\/Vim fan will tell you that you can do all of the above with it, but that falls into the same category as filling in the Grand Canyon with a teaspoon. You&nbsp;<em>can&nbsp;<\/em>do it, but why on earth would you want to?<\/p>\n\n\n\n<p><strong>What Vim Does Best<\/strong><\/p>\n\n\n\n<p>Vim edits text. &nbsp;Plain\u2026text. &nbsp;Not pretty bold, italicized, with all sorts of alignment characters and strange paragraphs and pagination doohickeys\u2026 no, Vim just makes text files. &nbsp;Text files that are SO devoid of bells and whistles, in fact, that when you open a Vim created file in your favorite WYSIWYG editor, you\u2019ll see what appears to be a pile of letters and such all crammed together like you had nothing else on your keyboard but letters, numbers, and punctuation.<\/p>\n\n\n\n<p>Vim allows you to eliminate all the cruft and get right down to the matter of creating plain, unencumbered text files.<\/p>\n\n\n\n<p><strong>Why Does it Matter?<\/strong><\/p>\n\n\n\n<p>When you\u2019re logged onto your favorite Linux through two bastion hosts across several continents and have latency to boot, WordPad will not help. &nbsp;You\u2019ll need a lightweight text editor within which you can load, edit, and save the single most numerous type of item on a UNIX system\u2026 a text file.<\/p>\n\n\n\n<p><strong>Where Can I learn More About Vim?<\/strong><\/p>\n\n\n\n<p>Vim\u2019s main project page can be found&nbsp;<a href=\"http:\/\/www.vim.org\">here<\/a>&nbsp;online. &nbsp;The main page has links to documentation and various community links as well as connections to various types of plugins and add ons you can use with Vim for any number of tasks. &nbsp;You can join online forums, mailing lists, and communities whose entire purpose is the extension and promotion of Vim. &nbsp;But that\u2019s not what we\u2019re up to\u2026<\/p>\n\n\n\n<p><strong>How We Will Use Vim<\/strong><\/p>\n\n\n\n<p>For our purposes, we will use vim as code editor. &nbsp;No more, no less. &nbsp;Vim\u2019s abilities can help us&nbsp;<em>see&nbsp;<\/em>our code in ways that let\u2019s us know when there\u2019s an issue and can direct us generally in the direction of our solutions. &nbsp;So, let\u2019s dive in to a minor Vim tutorial. &nbsp;(If you\u2019re an advanced Vim user, stick with me\u2026)<\/p>\n\n\n\n<p>All Linux distributions and Mac OSX come with Vim pre-installed and ready for action. &nbsp;Note that some distributions of Linux will have&nbsp;<em>both&nbsp;<\/em>Vim and Vi. &nbsp;You will either need to get into the habit of running \u201cvim\u201d from the command line, or setup your shell aliases to load Vim every time you type \u201cvi\u201d instead.<\/p>\n\n\n\n<p>When you launch Vim, you see a screen much like the following. &nbsp;I use Mac OSX, but the effect is the same, regardless of platform.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"900\" height=\"679\" src=\"https:\/\/i0.wp.com\/questy.org\/wp-content\/uploads\/2024\/08\/vim.png?resize=900%2C679&#038;ssl=1\" alt=\"\" class=\"wp-image-267\"\/><\/figure>\n\n\n\n<p>I have several features turned on (including the line across the bottom that provides me a lot of information about the file I\u2019m editing), but the main things we will talk about here are&nbsp;<em>syntax highlighting<\/em>&nbsp;and&nbsp;<em>plugins&nbsp;<\/em>after a short tutorial on how Vim works.<\/p>\n\n\n\n<p><strong>I Can\u2019t DO Anything!!!<\/strong><\/p>\n\n\n\n<p>Most people\u2019s frustrations begin right on this page. &nbsp;From here, nothing seems familiar. &nbsp;I can\u2019t pull down a menu and I can\u2019t really even choose \u201cexit\u201d from a list of things to do. &nbsp;The only real hint I have is on the screen above:<\/p>\n\n\n\n<p>type &nbsp;:q&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; to exit<\/p>\n\n\n\n<p>\u2026which is quite a peculiar directive. &nbsp;Why do I have to type a colon? &nbsp;What does it mean? &nbsp;And goodness help you if you already managed to type something into the screen. &nbsp;The instructions you see above disappear, and without the right collection of keystrokes, you\u2019re not getting out of Vim. &nbsp;You\u2019ll most likely just close the window and start looking for \u201cnotepad\u201d.<\/p>\n\n\n\n<p>Here\u2019s where the tutorial starts.<\/p>\n\n\n\n<p>Vim is what is known as a full-screen text editor. &nbsp;It started back in the Amiga days, and was first released publicly in 1991. &nbsp;Before Vi\/Vim, to create text files, there were line-based text editors that only allowed you to see one line of a file at a time. &nbsp;So, you really couldn\u2019t work with huge files\u2026it would be difficult to see file lines compared to each other or look at the whole flow of a subroutine you had written, or even to match wording or syntax from one section to another.<\/p>\n\n\n\n<p>Enter the full-screen editor.<\/p>\n\n\n\n<p>What the full-screen editor did was open a file on disk, reserving space in memory to hold the entire file, and then display to you a \u201cwindow\u201d into your file equal to your terminal\u2019s display size. &nbsp;for instance, I have a terminal right now that has a several thousand line file open. &nbsp;Of that file, I can only see 25 lines and 80 columns wide. &nbsp;This \u201cwindow\u201d onto my file is something I personally configured in my terminal program (in my case, \u201ciTerm\u201d). &nbsp;I can scroll up and down this file, sliding forward or backward within the file from the beginning to the end (just as you may an MS Word document) and can interact with\/edit any character I can see on the screen. &nbsp;(we\u2019ll talk about search and replace and other such things later).<\/p>\n\n\n\n<p>We are currently in what is known as \u201ccommand mode\u201d. &nbsp;Many would ask why you don\u2019t just call this \u201cview\u201d mode, and the reasons are very simple. &nbsp;From this screen, you issue MANY commands to Vim and tell it how you want it to behave for you. &nbsp;For our purposes, we will use \u201ccommand\u201d mode and \u201cinsert\u201d mode mostly.<\/p>\n\n\n\n<p><strong>Well, How Do I Edit Something?<\/strong><\/p>\n\n\n\n<p>To edit your file, you enter into a mode known as \u201cinsert\u201d mode. &nbsp;From this page there are several different modes you can enter, but \u201cinsert\u201d mode is the easiest. &nbsp;You simply type a single lowercase \u201ci\u201d to enter this mode. &nbsp;When you do so, a cursor appears on the top line of the page you are viewing, and you are now able to type all you like. &nbsp;Letters,&nbsp;keys, tabs\u2026 all normal typing idioms are available to you from this point. &nbsp;How, you may ask, then do you save your work to disk? &nbsp;I\u2019m in this \u201cinsert\u201d mode and don\u2019t know how to save!<\/p>\n\n\n\n<p>Think about what you wish to accomplish\u2026 you wish to issue the command \u201csave\u201d to Vim. &nbsp;Command\u2026 &nbsp;as in\u2026 \u201ccommand mode\u201d, perhaps? &nbsp;Well, we have to go back into command mode, then, so we can issue some commands to Vim and exit the program.<\/p>\n\n\n\n<p>Any time you are in Vim, your \u201csaving grace\u201d is your [esc] key. &nbsp;Two taps on the escape key lways brings you back to command mode from anywhere. &nbsp;Go ahead and try it.<\/p>\n\n\n\n<p>You\u2019ll now notice you have returned to \u201ccommand mode\u201d just as you were when you first opened Vim. &nbsp;The only difference is that everything you typed is now on your screen and has not gone away. &nbsp;Your \u201cedit buffer\u201d is full of a file you now can do things with. &nbsp;You can save it, delete it, save it out as a specific file name\u2026 a myriad of normal file operations you may be used to from other software packages.<\/p>\n\n\n\n<p>In our case, we want to just save the file. &nbsp;However, when we opened Vim, we didn\u2019t specify a file name to edit, we just opened Vim. &nbsp;So, for all intents and purposes, we have an open buffer full of \u201cstuff\u201d and no file name to associate with it. &nbsp;What we want to do now is to \u201cwrite\u201d the file to disk. &nbsp;To do so, we have to issue commands to Vim in Command mode.<\/p>\n\n\n\n<p><strong>Command Mode<\/strong><\/p>\n\n\n\n<p>To issue commands to Vim, we have to tell it we are issuing it a command, otherwise you may hit a letter than means something else. &nbsp;Recall that simply by hitting a lowercase \u201ci\u201d we placed vim into insert mode and then by hitting the [esc] key twice, we left it. &nbsp;Clearly, there\u2019s more to this editor than we can readily see, so how do we save the file?<\/p>\n\n\n\n<p>When you have a buffer with text you would like to save, you have to first hit a colon \u201c:\u201d. &nbsp;You\u2019ll notice that Vim places the colon on the bottom line of your screen to the left, awaiting a command. &nbsp;While there are several commands we can perform here (as well as joining multiple commands together), we will simply write the file right now. &nbsp;To do so, while at the \u201ccolon prompt\u201d, we simply type:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code class=\"\">**: w foo.txt**<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>and press the \u201center\u201d key. &nbsp;You will receive a message on the last line that lets you know the file has been written to disk:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td><code class=\"\">**\"foo.txt\" [New] 1L, 16C written**<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>But I\u2019m still in Vim. &nbsp;What do I do now?<\/p>\n\n\n\n<p>Just like \u201cw\u201d is a command, exiting the program also is a command. &nbsp;Guessably so, it is the letter \u201cq\u201d for \u201cquit\u201d. &nbsp;So, as before, you hit the colon key, then the letter q, and then the enter key. &nbsp;If all goes well, you\u2019ll be back at the command line.<\/p>\n\n\n\n<p><strong>Much More to This<\/strong><\/p>\n\n\n\n<p>Were I to do this for all the features of Vim, I\u2019d be writing a book. &nbsp;However, fortunately for you, there are several tutorials and cheat sheets on Vi\/Vim all over the Internet. &nbsp;Here are a few of my favorites.<\/p>\n\n\n\n<p><a href=\"http:\/\/vim.wikia.com\/wiki\/Tutorial\">The Main Vim Tutorial&nbsp;<\/a><a href=\"https:\/\/www.linux.com\/learn\/tutorials\/228600-vim-101-a-beginners-guide-to-vim\">Linux.com\u2019s Vim Tutorial<\/a><\/p>\n\n\n\n<p>As well as some cheat sheets for you to refer to for quick reference on the various commands available when using Vim:<\/p>\n\n\n\n<p><a href=\"http:\/\/www.viemu.com\/vi-vim-cheat-sheet.gif\">One of My Favorites&nbsp;<\/a><a href=\"http:\/\/vim.rtorr.com\">Another Good Cheat Sheet<\/a><\/p>\n\n\n\n<p>Take some time learning the basics of Vim before pressing on to the next article: \u201cCustomizing Vim\u201d.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Old School Why on earth am I starting with Vim? &nbsp;(or \u201cvi\u201d for you old folks) Vim is the modern \u201cvi\u201d implementation. &nbsp;A full-screen text editor with a myriad of options and abilities for beyond anything I could ever cover here. &nbsp;But Vim has one thing going for it that no other text editor has. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[6],"tags":[],"class_list":["post-266","post","type-post","status-publish","format-standard","hentry","category-technology"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":1,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":268,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/posts\/266\/revisions\/268"}],"wp:attachment":[{"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/questy.org\/index.php\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}