Debugging Vim Scripts

Ovid on 2008-05-01T08:31:52

Working a vim plugin someone sent me to test and it turns out to be buggy ... but only on Solaris. My MacBook has no such problem. Since I promised to debug it if it's not reproduceable. So to do that I'm working through the code, line by line, only to discover how limited my vim knowledge really is.

This little mapping will help with that. If, and only if, you are in a file which vim thinks has a 'vim' filetype, it will remap 'K' to automatically display help for any keyword the cursor is on. It's a much faster way of figuring things out (and it's actually in a filetype plugin rather than my .vimrc).

au! FileType vim :noremap K :exe "help " . expand("")

On a side note, I'm getting so irritated with vim that I started playing with Emacs. That led me quickly back to vim :(

Vim: making easy things possible and hard things Herculean.


Localize It

Smylers on 2008-05-01T09:55:45

If, and only if, you are in a file which vim thinks has a 'vim' filetype ...

However once you've edited a Vim file the mapping will persist and continue to apply to later non-Vim files that you edit. The fix is to make it a buffer-local mapping:

au! FileType vim :noremap <buffer> K :exe expand("help <cword>")<cr>

Re:Localize It

Ovid on 2008-05-01T10:12:08

D'oh! Thanks. I keep forgetting that.

There&#8217;s an official way for that

Aristotle on 2008-05-02T00:33:59

From my .vimrc:

autocmd FileType {vim,help} setlocal keywordprg=:help

I’m not sure there’s any real advantage to this method, since apparently keywordprg is only used for the “K” command anyway, but hey, it’s the blessed way…