<h2 id="introduction-to-vim">Introduction to Vim</h2>
<p>This is the start of the <em>Vim Hints</em> series.</p>
<p>As a Linux user there are many editors available to you. Which one you want to use depends on your needs and the amount of time you want to dedicate to learning how to use it.</p>
<p>One of the editors from the early days of Unix is <em>vi</em>, written in 1976. Contemporary with it is <em>Emacs</em>, also originating in 1976. However, it seemed to become the norm (in my experience anyway) that <em>vi</em> rather than <em>Emacs</em> was provided as standard with versions of Unix, and this has often continued into Linux.</p>
<p>I originally started using Unix around 1988 and found <em>vi</em> available to me. I learnt how to use it in a rudimentary way since I knew I'd find it on any Unix systems I came across.</p>
<p>Many derivatives and clones of <em>vi</em> have been created. The one which has become the most popular and available is <em>Vim</em>, the name of which is an acronym for <em>Vi IMproved</em>, created in 1991 by Bram Moolenaar. This is what I use, and I have not wanted to learn another editor since adopting it, even though I have experimented with several. This is the editor we will be looking at in this series.</p>
<h3 id="whats-the-series-about">What's the series about?</h3>
<p>The thinking behind this series is:</p>
<ul>
<li>You may already be using Vim; there are features you may not be aware of that can be revealed here</li>
<li>You may be using a different, simpler editor; you might want to use Vim and gain from its advanced features</li>
</ul>
<p>Of course, you may prefer to learn Emacs instead. That's fine; you should choose the tool that best suits your needs. Both Emacs and Vim have quite steep learning curves, but the broad range of capabilities you gain from knowing either is considerable.</p>
<p>I am not an expert in Vim. In fact I am continuing to learn new Vim features on a regular basis. However, I have been using it for many years and would like to share some of what I have learnt.</p>
<h3 id="why-use-vim">Why use Vim?</h3>
<p>With simpler editors you can move about a file, add, remove and change text and save the results. The editor might have syntax highlighting and some degree of knowledge of the programming language you are typing. You might have spell checking as well.</p>
<p>With Vim and other more advanced editors you have all of this and a lot more. You can perform global changes throughout a file, process many files at once, add plugins to the editor to change its behaviour, and so on. Also, there is a language behind the scenes which can be used to build extensions.</p>
<h3 id="using-vim">Using Vim</h3>
<p>Usually, typing the command <strong>vi</strong> at the command line actually invokes <em>vim</em>. Vim runs in vi-compatible mode by default, which results in Vim enhancements being unavailable.</p>
<p>Vim uses a configuration file, which is called <code>.vimrc</code> on Linux. (Vim will also run on Windows, OSX and other operating systems but we will not be covering these implementations in this series.) Vim also has a GUI interface invoked by the command <code>gvim</code>, and it has its own configuration file <code>.gvimrc</code>.</p>
<p>I you don't have a <code>.vimrc</code> create one with <strong><code>touch ~/.vimrc</code></strong> before you start. This will stop Vim running in vi-compatible mode. We will look at what the <code>.vimrc</code> can be used to do later.</p>
<p>You can start Vim on its own without pointing at a file, but normally you use it to edit a file, which need not already exist. So, to create a new file called <code>testfile</code> invoke Vim with the command: <strong><code>vim testfile</code></strong></p>
<p>Once running, Vim shows the contents of the file. All the lines on the screen where there is no content are marked with a tilde &quot;~&quot; character. If you are creating a file the first line on the screen will be blank, and last line will contain the name of the file followed by &quot;[New File]&quot; and some other details which we will examine later:</p>
<pre><code>&quot;testfile&quot; [New File]       0,0-1         All</code></pre>
<p>All the rest of the lines will contain a tilde.</p>
<p>Vim is a <em>modal</em> editor. The mode you usually start in is <em>normal</em> mode where you can move around the lines of the file and perform actions, but nothing you type is actually written to the file. In fact, the keys you type are actually editing commands. This is one of the features of Vim that causes problems for new users.</p>
<p>Since this is a new file there is not much you can do other than enter text, and to do this you need to switch to <em>insert</em> mode. Do this by pressing the <strong>i</strong> key. The message <strong>-- INSERT --</strong> will appear on the bottom line of the screen. Now type some text, pressing the <strong>Enter</strong> key at the end of each line.</p>
<p>You might notice that in <em>insert</em> mode you can press the arrow keys and move back to text you have already typed. This is a Vim feature and was not available in the original vi editor.</p>
<p>When you have finished entering text, press the <strong>Esc</strong> key to exit from <em>insert</em> mode. Now you can move around in <em>normal</em> mode, but remember that the keys you press are now commands not data to be entered into the file.</p>
<p>To move around in <em>normal</em> mode use the arrow keys or the home row keyboard keys: <strong>k</strong> to move up, <strong>j</strong> to move down, <strong>h</strong> to move left and <strong>l</strong> to move right.</p>
<p>This brings us to the last mode we'll look at: <em>command</em> mode. To enter this mode press the <strong>:</strong> (colon) key in <em>normal</em> mode. This moves the cursor to the last line of the screen, which starts with the colon you just typed. Here you can enter another class of commands. This time, we'll just look at how you can save the file and exit Vim.</p>
<p>Saving the file is achieved with the <strong>w</strong> command, and to exit from Vim the <strong>q</strong> command is used. These can be typed together, so <strong>:wq</strong> writes the file and exits.</p>
<p>If you were to use <strong>:q</strong> on its own, having entered data into Vim, this would not work. Vim prevents you from throwing away your work this way. If you really meant to quit without saving then the <strong>q</strong> must be followed by an exclamation mark (&quot;!&quot;). So <strong>:q!</strong> lets you exit Vim without saving.</p>
<h4 id="summary-so-far">Summary so far</h4>
<ul>
<li>Vim usually starts in <em>normal</em> mode</li>
<li>Arrow keys or <strong>h</strong>, <strong>j</strong>, <strong>k</strong> and <strong>l</strong> for left, down, up and right for navigation in <em>normal</em> mode</li>
<li><strong>i</strong> enters <em>insert</em> mode</li>
<li><strong>Esc</strong> exits from <em>insert</em> mode and reverts to <em>normal</em> mode</li>
<li><strong>:</strong> in <em>normal</em> mode enters <em>command</em> mode</li>
<li><strong>:w</strong> in <em>normal</em> mode writes the file</li>
<li><strong>:wq</strong> in <em>normal</em> mode writes and exits</li>
<li><strong>:q</strong> in <em>normal</em> mode exits but only if nothing was changed or added</li>
<li><strong>:q!</strong> in <em>normal</em> mode exits regardless of any changes</li>
</ul>
<h2 id="errata">Errata</h2>
<ul>
<li>I was wrong about the contents of the last line of the Vim screen in the audio. The notes have been corrected.</li>
</ul>
<h2 id="links">Links</h2>
<h3 id="history">History</h3>
<ul>
<li>Wikipedia page for <em>vi</em> <a href="https://en.wikipedia.org/wiki/Vi">https://en.wikipedia.org/wiki/Vi</a></li>
<li>Wikipedia page for <em>Emacs</em> <a href="https://en.wikipedia.org/wiki/Emacs">https://en.wikipedia.org/wiki/Emacs</a></li>
<li>Wikipedia page for <em>Vim</em> <a href="https://en.wikipedia.org/wiki/Vim_(text_editor)">https://en.wikipedia.org/wiki/Vim_(text_editor)</a></li>
<li>Wikipedia page for Bram Moolenaar <a href="https://en.wikipedia.org/wiki/Bram_Moolenaar">https://en.wikipedia.org/wiki/Bram_Moolenaar</a></li>
</ul>
<h3 id="books">Books</h3>
<ul>
<li><em>Learning the vi and Vim Editors, 7th Edition</em>, by Arnold Robbins, Elbert Hannah, Linda Lamb. <a href="https://shop.oreilly.com/product/9780596529833.do">https://shop.oreilly.com/product/9780596529833.do</a></li>
<li><em>vi and Vim Editors Pocket Reference, 2nd Edition</em>, by Arnold Robbins. Available to buy or as a free PDF. <a href="https://it-ebooks.info/book/144/">https://it-ebooks.info/book/144/</a></li>
<li><em>A Byte of Vim</em>, by Swaroop C H. Available as a free PDF. <a href="https://www.swaroopch.com/notes/vim/">https://www.swaroopch.com/notes/vim/</a></li>
<li><em>The Vim Tutorial and Reference</em>, by Steve Oualline. Available as a free PDF. <a href="https://www.oualline.com/vim-book.html">https://www.oualline.com/vim-book.html</a></li>
</ul>
<h3 id="other-resources">Other resources</h3>
<ul>
<li>Vim Community <a href="https://www.vim.org/">https://www.vim.org/</a></li>
<li>Vim Tips Wiki <a href="https://vim.wikia.com/wiki/Vim_Tips_Wiki">https://vim.wikia.com/wiki/Vim_Tips_Wiki</a></li>
</ul>
