Enable Spell Checking in Vim for Markdown and Git Commit Messages

<p>I had a ridiculous typo in a Git commit message recently, so I decided to explore spell checking in Vim. As it turns out, it&rsquo;s extremely easy.</p> <p>I only wanted it enabled for Git commit messages and markdown, so I added the following to my vimrc / init.vim:</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" Spell-check Markdown files and Git Commit Messages</span> autocmd <span class="nb">FileType</span> markdown <span class="k">setlocal</span> <span class="nb">spell</span> autocmd <span class="nb">FileType</span> gitcommit <span class="k">setlocal</span> <span class="nb">spell</span> </code></pre></div> <p>By doing this, it highlights potential misspellings in red underline. You can see a list of potential corrections by moving the cursor over the word in normal mode and pressing <code>z=</code>. Or you can add the word to the dictionary by pressing <code>zg</code>.</p> <p>While trying this out, I also discovered that you can enable dictionary auto-completion using Vim&rsquo;s normal <code>ctrl-n</code> and <code>ctrl-p</code>. To enable this (again, for select file types), add the following to your vimrc / init.vim:</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" Enable dictionary auto-completion in Markdown files and Git Commit Messages</span> autocmd <span class="nb">FileType</span> markdown <span class="k">setlocal</span> <span class="nb">complete</span><span class="p">+=</span>kspell autocmd <span class="nb">FileType</span> gitcommit <span class="k">setlocal</span> <span class="nb">complete</span><span class="p">+=</span>kspell </code></pre></div> <p>Then, while in insert mode, type part of a word and hit <code>ctrl-n</code> to start cycling through potential matches and <code>ctrl-p</code> to cycle through in reverse.</p> <p>Here&rsquo;s a quick demonstration of both spell check and dictionary auto completion:</p> <p><img src="/assets/images/vim_spell_check_and_dictionary_auto_complete-1155dd19.gif" alt="Vim Spell-Checking and Auto Complete" /></p>