Lookup pydoc using Vim keywordprg

Posted on

If you use Vim to exercise your python-fu and find yourself constantly switching between your vim window and a console to run pydoc to lookup the signature or usage of a function, this one’s definitely for you!

Vim’s normal mode K command uses keywordprg to lookup the keyword under the cursor. By default this is set to man which is rarely useful unless you’re into some heavy kernel/system hacking. This can easily be switched to the lookup program of your choice (in our case pydoc) using the following command.

autocmd BufNewFile,BufRead *.py set keywordprg=pydoc

This will simply set keywordprg to pydoc whenever you create an new file, or open an existing file, with a .py extension. Then, you can simply hit K to open up the pydoc page for the object under the cursor. This also works in visual mode; so, to lookup the pydoc for a module or a function inside a module, simply select the entire keyword and hit K.

Note:
This works seamlessly with Vim running in a fully functional *nix terminal. However, I’m having a little trouble getting this to behave in GVim. Without a fully functional terminal, the output simply scrolls off the top without the ability to scrollback. Setting more and more-prompt don’t seem to help either.

References:
http://vimdoc.sourceforge.net/htmldoc/various.html#K
http://vimdoc.sourceforge.net/htmldoc/options.html#’more
http://vimdoc.sourceforge.net/htmldoc/message.html#more-prompt

Leave a Reply

Your email address will not be published.