<h2 id="vim-ate-my-homework">Vim Ate my Homework</h2>
<p>In this episode I want to look at how to keep your work secure with Vim. Next episode we will look at how to create and edit files.</p>
<h3 id="avoiding-data-loss-with-a-backup">Avoiding data loss with a backup</h3>
<p>The best place to start is with the configuration file which we met last episode. As we saw, this is usually <code>$HOME/.vimrc</code>. However, it can also be <code>$HOME/.vim/vimrc</code>, which is actually recommended since it keeps all Vim files in the same place. I use the former, since that's the way I have always done it.</p>
<p>Let's add some options to this file. Configuration options consist of <em>command</em> mode commands. Actually, to be precise about it, any <a href="https://en.wikipedia.org/wiki/Vim_script">Vim Script</a> expression may be written there.</p>
<p>First it's a good idea to ensure that Vim runs with all of its standard features enabled. The option for this is called <strong><code>compatible</code></strong> (meaning compatible with Vi), which we need to turn off. This is done with the option:</p>
<pre><code>set nocompatible</code></pre>
<p>Next, it's highly advisable to make Vim generate a backup file whenever it opens a file for editing. The backup file has the same name as the original file with a tilde appended. The configuration command is:</p>
<pre><code>set backup</code></pre>
<p>The backup file is a copy of the file which existed before editing started.</p>
<p>By default the backup file is saved in the same directory as the file being edited. If this is a problem (and to me this is not), then it is possible to tell Vim to save backups in a fixed place. This is done with the command <strong><code>set backupdir=</code></strong> followed by a list of directories. For example:</p>
<pre><code>set backupdir=~/.backup,.,/tmp</code></pre>
<p>If you were to add this to your configuration file, Vim would save backups in a top-level directory <code>~/.backup</code> (which must already exist), then if this fails it will save in the current directory, falling back to <code>/tmp</code> if all else fails. Whether you do this is up to you. I would suggest you do not, at least not until you are more experienced with Vim.</p>
<h3 id="undoing-and-redoing-changes">Undoing and redoing changes</h3>
<p>Vim can undo changes you make to a file. This is useful if a change was the wrong change or in the wrong place. It can also redo the undone change.</p>
<p>The <strong>u</strong> command in <em>normal</em> mode undoes the last change. The <em>redo</em> function is invoked by pressing the <em>Ctrl</em> key while pressing <strong>r</strong>. This key sequence is normally represented as <strong>CTRL-R</strong>.</p>
<p>Vim keeps a record of the changes, so successive <strong>u</strong> commands undo successive changes back in time. Conversely, <strong>CTRL-R</strong> redoes the undone changes forward in time.</p>
<p>Normally the change history is lost when Vim exits, but two configuration options can be used to save it. The <strong>undofile</strong> option ensures change history is written to a file and <strong>undodir</strong> shows the (pre-existing) directory which is to hold these files.</p>
<pre><code>set undodir=~/.vim/undodir
set undofile</code></pre>
<p>It can be a little surprising if you press <strong>u</strong> in a file you have just opened in Vim to find that it undoes something you changed last time you edited it! However, on the whole I think this is a great feature.</p>
<h3 id="file-recovery">File recovery</h3>
<h4 id="the-swap-file">The Swap File</h4>
<p>By default Vim uses a recovery mechanism where it generates a <strong>swap file</strong>. Under Unix and Linux this file has a name built from the name of the file being edited with a dot prepended (making it a hidden file) and with the extension &quot;.swp&quot;. So, if you were editing the file <code>testfile</code> the swap file would be a file called <code>.testfile.swp</code> in the same directory.</p>
<p>It is possible to make Vim write the swap file elsewhere, such as on another partition. You can also turn this recovery capability off. It is probably advisable to use the default settings while you are learning Vim.</p>
<p>The swap file is updated after typing 200 characters or when you have not typed anything for four seconds. The swap file is deleted as soon as Vim stops editing the file.</p>
<h4 id="recovery">Recovery</h4>
<h5 id="case-1-there-are-changes-in-the-swap-file">Case 1: there are changes in the swap file</h5>
<p>If something bad happens during an editing session, such as the loss of power, the swap file will remain after the event. If you know that you need to recover your edit session then you can simply type the following in the directory where the file you were editing exists:</p>
<pre><code>vim -r filename</code></pre>
<p>You will see a message such as the following:</p>
<hr />
<pre><code>Using swap file &quot;.testfile3.swp&quot;
Original file &quot;~/testfile3&quot;
Recovery completed. You should check if everything is OK.
(You might want to write out this file under another name
and run diff with the original file to check for changes)
You may want to delete the .swp file now.

Press ENTER or type command to continue</code></pre>
<hr />
<p>See the explanation on the <a href="https://vim.wikia.com/wiki/Recovering_files">Vim wiki</a>.</p>
<p>Alternatively, when you try to edit a file you were editing at the time of the failure Vim will detect the presence of a swap file and alert you with a message such as:</p>
<hr />
<pre><code>E325: ATTENTION
Found a swap file by the name &quot;.testfile2.swp&quot;
          owned by: hprdemo   dated: Fri Feb 13 15:33:41 2015
         file name: ~hprdemo/testfile2
          modified: YES
         user name: hprdemo   host name: i7-desktop
        process ID: 16181
While opening file &quot;testfile2&quot;
             dated: Sat Dec  6 18:34:32 2014

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use &quot;:recover&quot; or &quot;vim -r testfile2&quot;
    to recover the changes (see &quot;:help recovery&quot;).
    If you did this already, delete the swap file &quot;.testfile2.swp&quot;
    to avoid this message.

Swap file &quot;.testfile2.swp&quot; already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:</code></pre>
<hr />
<p>Vim here is indicating that there are unsaved changes that can be recovered. It is also warning that if someone is editing the same file (such as you in another window) this might account for the presence of the swap file.</p>
<p>Assuming it's appropriate, you can recover the changes and continue editing by pressing <strong>r</strong> at the above prompt. You will see messages such as the ones above relating to the <code>vim -r filename</code> example.</p>
<p>Be aware that if you continue editing the original swap file will continue to exist and you will get the same message again next time you edit the file. Vim will create a new swap file (called <code>/home/hprdemo/.testfile2.swo</code> in this case) to protect the new editing session.</p>
<p>This situation can be a little confusing if you have not encountered it before. There are a number of ways you can resolve this:</p>
<ol type="1">
<li><p>You can save the recovered file and exit Vim (type <strong>:wq</strong>). You can then edit the same file all over again. You will see almost the same message as before, but you can now delete the swap file by pressing <strong>d</strong>. The message you see the second time round will contain the additional warning that the file you are editing is newer than the swap file - that is because you just saved a new copy of it!</p></li>
<li><p>You can save the file and exit Vim as above, but then explicitly delete the swap file. In the example you would do this by typing: <strong><code>rm    .testfile2.swp</code></strong></p></li>
<li><p>As before you can save the file but this time without exiting Vim (type <strong>:w</strong>). Then tell Vim to re-edit the current file with the command <strong>:e</strong>. You will then see the warning about there being a swap file, and you can type <strong>d</strong> to delete it.</p></li>
</ol>
<h5 id="case-2-there-are-no-changes-in-the-swap-file">Case 2: there are no changes in the swap file</h5>
<p>If, when you see the message about finding a swap file you see that there are no changes to recover you can just delete the swap file by pressing <strong>d</strong>. You can then continue with editing the file as normal.</p>
<h4 id="dont-panic">Don't Panic!</h4>
<p>This recovery process is complex because Vim is trying to ensure that you are protected against losing your changes.</p>
<p>As it says in the <a href="https://vimdoc.sourceforge.net/htmldoc/recover.html">Vim manual</a> DON'T PANIC!</p>
<h3 id="summary">Summary</h3>
<ul>
<li>The configuration file should contain the following:</li>
</ul>
<pre><code>set nocompatible
set backup
set undodir=~/.vim/undodir
set undofile</code></pre>
<ul>
<li>Use <strong>u</strong> in <em>normal</em> mode to undo a change</li>
<li>Use <strong>CTRL-R</strong> in <em>normal</em> mode to redo an undone change</li>
<li>Re-starting Vim after a crash will invoke a recovery dialogue</li>
</ul>
<h2 id="links">Links</h2>
<ul>
<li>Vim Script (vimscript or viml) <a href="https://en.wikipedia.org/wiki/Vim_script">https://en.wikipedia.org/wiki/Vim_script</a></li>
<li>Recovering files (Vim Wiki) <a href="https://vim.wikia.com/wiki/Recovering_files">https://vim.wikia.com/wiki/Recovering_files</a></li>
<li>Recovery after a crash <a href="https://vimdoc.sourceforge.net/htmldoc/recover.html">https://vimdoc.sourceforge.net/htmldoc/recover.html</a></li>
<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>
