Vim: The Hidden Power of Customization

If I had to pick my favorite vim customizations, my spell-check key binding would be definitely one of my top picks. Believe it or not, Vim makes me super-efficient at writing not only code, but also prose.

Like most editors out there Vim has an in-line spell-check feature. If you happen to use the graphical version of Vim (rather than the command line one) it works about as you would expect. Misspelled words are underlined, and you right-click on them to choose a correction. Of course, being a console application first and foremost, Vim has key-bindings to correct spelling without ever touching the mouse.

For example the keystroke [ s jumps to the nearest misspelled word to the left of the cursor. The z = opens up list of suggested corrections for said word. While not the easiest combination to remember, it is still faster than taking your hand off the keyboard and correcting the word using mouse, or “arrow keying” over to fix it manually like you would do in Word.

Vim being what it is, lends itself to endless customization. I found myself hitting this particular combination all the time to the point that I was fed up with how many key-strokes it required. So I decided to change it by adding these two lines to my .vimrc:

inoremap <C-l> <esc>[sz=
nnoremap <C-i> gi

What does this do? It binds the “jump to last misspelled word and show suggestions” to a single key stroke: Ctrl+l. This means that as I type my blog posts I can hit that combo at any time in order to correct the most recently misspelled word. Of course this puts the cursor on said word, and thus takes me away from where I was typing before. Usually I can just hit A to simply jump to the end of the line, but sometimes when I want to correct a word, I’m revising an existing sentence. So I created a second binding which lets me hit Ctrl+i to jump back to my last insertion point.

The combination of these two key-bindings lets me do in-line corrections much faster than in any other editor. When I type, I usually don’t want want to stop for each misspelling – I usually want to be able to finish typing the thought, or at the very least the current word. In most cases I’m not ready to correct my typo till I’m about 3-4 words away from the spot it was made. This jump, then jump back technique is extremely useful and addictive. I tend to miss it quite a bit when I’m typing in a web browser or in an other editor.code

Here is another example for you. When I write these blog posts, I usually do it in Vim, via the It’s All Text Firefox add-on. Since I usually like to inject some HTML formatting into my posts, I have the following line in my .vimrc

au BufRead,BufNewFile *.txt setfiletype html

This forces Vim to treat text files like HTML files for the purposes of syntax highlighting and plug-in use. Which means my posts become instantly more readable in their source form. In addition I use the excellent SnipMate extension. When I want to emphasize word or a sentence, I can just type emp hit tab, and vim will generate the code for me and I can just continue typing.

Sometimes however I will forget. Or decided to emphasize/bold a word, or make something into a block quote later on. That’s where SnipMate does not really work. So you have to manually go in and add the HTML code. For example, if I wanted to emphasize this word I would need to place it under my cursor, hit c i w (which stands for change, internal word – and essentially deletes the word under the cursor) type in <emp>. Then I would hit Ctrl+r " which would paste the contents of the unnamed register (“), and thus get my deleted word back, and manually type in the closing HTML tag. Or, I could bind all of that to a single keystroke like , e like this:

" surround word/sentence with emp, strong
noremap <leader>e ciw<emp><C-r>"</emp><esc>
noremap <leader>E cis<emp><C-r>"</emp><esc>
 
noremap <leader>s ciw<strong><C-r>"</strong><esc>
noremap <leader>S cis<strong><C-r>"</strong><esc>
 
noremap <leader>bl I<blockquote><esc>A</blockquote><esc>

As you can see, I have these keys defined for emp, strong and blockquote tags. Lower case letters surround the word, upper case letters surround the entire sentence. With Blockquote I surround the entire line.

I have a similar set of bindings for Markdown:

" Markdown bindings
nnoremap <silent> <leader>h1 YpVr=
nnoremap <silent> <leader>h2 YpVr-
 
noremap <leader>b ciw**<C-r>"**<esc>
noremap <leader>B cis**<C-r>"**<esc>
noremap <leader>i ciw*<C-r>"*<esc>
noremap <leader>I cis*<C-r>"*<esc>

The last 4 lines are exactly as the HTML examples. The first two are slightly different, although I believe I mentioned this trick before. Markdown traditionally does not care how well you underline the words – as long as you have at least three dashes or equals symbols below a line, it will treat it as a heading. Some people stick with that, or just use the hash-mark syntax to be minimalistic. I like fully underlined headings because they look nice. You can easily do that in Vim using the above combo. The Y key copies the current line, p pastes it below, V selects the newly pasted line, r activates the “replace” command and uses = as an argument. Of course in my case this is , h 1.

This is the power of Vim – the combination of modal editing, powerful commands, extensions and incredible flexibility when it comes to configuration. People like to rag on Vim for it’s cryptic, and often complex command sequences, but they forget that no one expects you to use these sequences every day. Vim provides you with an incredibly powerful toolkit of general commands, but it also asks you to define your own shortcuts. The leader key (by default \ but most people rebind it to ,) exists for that very purpose – to allow you abbreviate long command sequences that you use often to 2-3 keystroke combos.

You make Vim your own. You make it conform to your coding style and your preferences. Unlike most software tools out there, Vim’s functionality is shaped by your needs, and not the other way around. It is a tool that can be custom tailored to your very specific set of requirements.

This entry was posted in technology and tagged . Bookmark the permalink.



2 Responses to Vim: The Hidden Power of Customization

  1. I refuse to customize my editor with anything but Lisp! :-)

    Reply  |  Quote
  2. Luke Maciak UNITED STATES Google Chrome Linux Terminalist says:

    @ Chris Wellons:

    Well, to be honest elisp seems much more pleasant way to program than the mess that is VimL :P

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>