Vim Recipes Searching Looking up Documentation for the Keyword Under the Cursor

Looking up Documentation for the Keyword Under the Cursor

Problem

You want to invoke an external command to lookup documentation for the keyword underneath the cursor. For example, Linux users may like to read the manual for the named command with the man utility.

Solution

Use :set keywordprg=program, then hit K while hovering over the word.

Discussion

This recipe calls the command specified with :set keywordprg, passing the current wordword is used in the sense of a string that looks like a word to Vim; it is not necessarily a valid word in your language. as an argument. Thus, if keywordprg = man, then hovering over the word ls and hitting K would display the documentation for Linux's ls command.

When used with man, Vim translates a count for the K command into a section number. So 7K over glob invokes man 7 glob to display section 7 of the glob documentation.

The Ruby programming language has a utility called ri that displays documentation about the given Ruby method. The Perl programming language has a similar command called perldoc. By setting keywordprg appropriately, you can make context-sensitive documentation lookup trivial.