Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (38 sloc) 2.63 KB
layout: post
title: Highlight debug code in Vim
date: 2014-01-14 11:40:00
updated: 2014-01-14 11:40:00
coordinates: 51.296964 4.429939
proofread: yes
<div class="narrow-col">
<p>I have been looking for a way to prevent myself from committing debug code to project repositories. I add <code>console.log</code> statements to debug problems all the time. This is no big deal if I would only remember to remove those lines before committing the patch.</p>
<p>My first approach was to add a Git <a href="" alt="What are Git hooks?">pre-commit hook</a> to warn me when I was ready to commit something stupid. While this works, it&apos;s a bit too intrusive for my liking. It also happens at the wrong moment. The pre-commit hook would only warn me when I was committing something, not right there when I was looking at the file.</p>
<p>Last week, <span class="vcard"><span class="fn">Wynn Netherland</span> posted a way to highlight <a class="url" href="" title="Words to avoid in tech writing post by Wynn Netherland">words to avoid in tech writing</a></span>, isn&apos;t that convenient?</span></p>
<p>I shamelessly took his snippet and modified it to highlight words that I don&apos;t want to commit:</p>
{% highlight vim %}
highlight DebugCode ctermfg=red guifg=red
match DebugCode /\cconsole/
autocmd BufWinEnter * match DebugCode /\cconsole/
autocmd InsertEnter * match DebugCode /\cconsole/
autocmd InsertLeave * match DebugCode /\cconsole/
autocmd BufWinLeave * call clearmatches()
{% endhighlight %}
<p>Here is how it looks with the light <a href="" alt="Solarized color theme">Solarized</a> theme:</p>
<figure class="extended-col">
<img src="../../assets/vim-highlight.png" alt="Screenshot of highlighted words in Vim" style="border: 5px solid #efefef" />
<div class="narrow-col">
<p>You will want to make sure that you enabled syntax highlighting. I was missing the <code>syntax enable</code> line and it wouldn't work.</p>
{% highlight vim %}
syntax on
syntax enable
{% endhighlight %}
<p>There you go, big red boxes for lines you might want to remove before pushing.</p>
<p>A small extra tip: using Vim <a href="" title="Using abbreviations @ Vim wiki">abbreviations</a>, from my <a href="" title="My .vimrc file"><code>.vimrc</code></a> file, will help you type longer commands:</p>
{% highlight vim %}
abbr clog console.log
{% endhighlight %}
You can’t perform that action at this time.