Using only vanilla Vim and complementary to tags
-based solutions,
K
calls 'keywordprg'
(defaults to man
) on the word under the cursor (in normal mode) or the current selection (in visual mode) and displays the output. As recommended in another answer, this can be used to refer to the documentation for standard functions and variables (some IDEs might blur the line between documentation for standard and non-standard libraries, you will certainly not get the latter from K
/man
).
However, as missed by that answer, pressing K
on printf
will output the documentation of printf(1)
("the command printf
"), not printf(3)
("the function printf
" from the C Standard Library) which you are probably after. See What do the numbers in a man page mean? for more details about this distinction. Practically speaking, it is the output of man [-s] 1 printf
(to which man printf
defaults) instead of man [-s] 3 printf
.
As stated in its documentation (:help kp
), K
takes a count:
'keywordprg' 'kp' string (default "man" or "man -s")
Program to use for the |K| command.
[...]
When "man", "man -s" or an Ex command is used, Vim will automatically
translate a count for the "K" command and pass it as the first
argument. For "man -s" the "-s" is removed when there is no count.
Therefore, you can get the documentation for printf(3)
from 3K
.
As this is likely to be the desired behaviour every time K
is used, I see 2 options:
and, as this behaviour is only useful in C code, add your choice among the solutions above to a ftplugin
in your after-directory
(e.g. to ~/.vim/after/ftplugin/c.vim
). Also note that, according to @muru's comment, the packages containing the manpages might have to be installed beforehand.
Finally, as mentioned by @kyticka, for C++ (although also helpful in C), you may use a similar approach by replacing man
with cppman
as your 'kp'
e.g. in ~/.vim/after/ftplugin/cpp.vim
:
set keywordprg=cppman
Alternatively, cppman
can be used to cache entries from cplusplus.com as (offline) man
pages and man
can be set up to read those pages, allowing you to use the default to access documentation for the C++ Standard Library from man
and, by extension, from the default 'kp'
.