vimtricks.wiki Concise Vim tricks, one at a time.

How do I make the K key look up documentation in a language-specific tool instead of man pages?

Answer

:set keywordprg

Explanation

The K key in normal mode looks up the word under the cursor using the program defined by keywordprg. By default this is man, but you can point it at any documentation tool — or even an Ex command — to get context-relevant help without leaving Vim.

How it works

:set keywordprg=man             " default: look up in man pages
:set keywordprg=python3\ -m\ pydoc  " Python docs via pydoc
:set keywordprg=ri              " Ruby docs via ri
:set keywordprg=:help           " Use Vim's own :help (note the : prefix)

When keywordprg starts with :, Vim treats it as an Ex command, appending the keyword as an argument. This means :set keywordprg=:help makes K run :help word — perfect when editing Vimscript.

Example

For a Vimscript development workflow, set this globally or via an ftplugin:

" In ~/.vim/ftplugin/vim.vim or ~/.config/nvim/ftplugin/vim.lua:
setlocal keywordprg=:help

Now pressing K over setline opens :help setline(), over BufReadPost opens :help BufReadPost, and so on — with no typing required.

Tips

  • Use setlocal in ftplugin files to set this per filetype without affecting other buffers
  • In Neovim, LSP plugins typically override keywordprg with hover() on LspAttach — check :verbose set keywordprg? to see the current value and where it was set
  • Pass multiple words by combining with a shell script if the tool supports it
  • :set keywordprg=:Man works with Neovim's built-in :Man command for section-aware man page display

Next

How do I briefly highlight text after yanking it to get visual confirmation of what was copied?