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

How do I look up documentation for the keyword under the cursor?

Answer

K

Explanation

Pressing K in normal mode runs a lookup program on the word under the cursor. By default it opens the man page, but you can customize keywordprg to use any documentation tool. This is a powerful way to get instant help on functions, commands, or keywords without leaving Vim.

How it works

  • K takes the word under the cursor and passes it to the program set in keywordprg
  • By default, keywordprg is set to man, so K on printf opens man printf
  • In Vim help files, K opens :help for the keyword instead
  • {count}K passes the count as the man section number (e.g., 3K on printf opens man 3 printf)

Example

With the cursor on strftime in a C file:

char *result = strftime(buffer, sizeof(buffer), "%Y-%m-%d", tm);
                ^cursor here

Pressing K opens man strftime showing the full documentation.

Tips

  • For Python files: :setlocal keywordprg=python3\ -m\ pydoc to look up Python docs with K
  • For Vim files: keywordprg is automatically set to :help
  • For Ruby: :setlocal keywordprg=ri to use Ruby's documentation tool
  • Use visual K to look up the visually selected text instead of just the word
  • Set keywordprg per filetype in ~/.vim/after/ftplugin/ for language-specific docs

Next

How do I return to normal mode from absolutely any mode in Vim?