Highlight debug code in Vim

Automatically highlight words in Vim to spot them easily.

I have been looking for a way to prevent myself from committing debug code to project repositories. I add console.log 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.

My first approach was to add a Git pre-commit hook to warn me when I was ready to commit something stupid. While this works, it’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.

Last week, Wynn Netherland posted a way to highlight words to avoid in tech writing, isn’t that convenient?

I shamelessly took his snippet and modified it to highlight words that I don’t want to commit:

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()

Here is how it looks with the light Solarized theme:

Screenshot of highlighted words in Vim

You will want to make sure that you enabled syntax highlighting. I was missing the syntax enable line and it wouldn’t work.

syntax on
syntax enable

There you go, big red boxes for lines you might want to remove before pushing.

A small extra tip: using Vim abbreviations will help you type longer commands:

abbr clog console.log