Last Updated: February 25, 2016
·
2.568K
· Saager Mhatre

Lookup pydoc using Vim keywordprg

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

1 Response
Add your response

Maybe things would work better from GVim if you used vim as a pager:

export PAGER="/bin/sh -c \"unset PAGER;col -b -x | \
vim -R -c 'set ft=man nomod nolist' -c 'map q :q<CR>' \
-c 'map <SPACE> <C-D>' -c 'map b <C-U>' \
-c 'nmap K :Man <C-R>=expand(\\"<cword>\\")<CR><CR>' -\""

This incantation turns all man pages into Vim editing sessions. Say goodbye to less!

over 1 year ago ·