20

Most editors have a feature like when you press some keys by placing caret over a function, it pops the documentation of that function along with number and type of arguments that function takes.

I was wondering whether Vim offers such support. For example, while coding in C, I use a built in function qsort(). Since I am not sure about type and number of arguments it takes, I would like to know it without escaping Vim. Is it possible?

  • 2
    Not sure this is exactly what you want, but if you give the right value to the option 'keywordprg' (the name of a program which can handle the documentation for the language you want), then by hitting K on a keyword (which can be the name of a function), you should get the info you want. For example, if you install cppman (github.com/aitjcize/cppman) and add an autocmd inside your vimrc such as autocmd FileType cpp setlocal keywordprg=cppman, then cppman should handle the documentation inside a c++ buffer. Not tested though. – saginaw Jan 23 '16 at 18:33
  • 2
    @saginaw While cppman is an excellent suggestion, if you have the C and C++ manpages (manpages-posix-dev and libstdc++-X.Y-doc packages on Debian-based systems) installed, plain old man should work. – muru Jan 23 '16 at 23:53
11

If you can set up YouCompleteMe, it supports C, C++, JavaScript, Go and some other languages. Here's it in action:

enter image description here enter image description here

It's a bit annoying to install, and once it's installed you need to configure it for C and C++ by creating a .ycm_extra_conf.py. I lifted one from this repo, and got this effect.

| improve this answer | |
  • 5
    "t's a bit annoying to install,"... That's an understatement. Still, it is very good. ^_~ – Sardathrion - against SE abuse Jan 28 '16 at 9:27
  • 4
    Is there any way to force the completions pop-up to remain opened while the function arguments are typed? For example, as soon as you type the first letter of the first argument qsort(a, the pop-up window disappears, making it difficult to remember the next arguments. – thiagowfx Jun 28 '16 at 21:02
  • 1
    But it just shows the prototype of the function. How to make it show its doxygen comments (like Eclipse does with javadocs)? I mean parameters, output, notes, refs... – Zeta.Investigator Apr 13 '18 at 8:33
  • 1
    @Zeta.Investigator One thing I use heavily in Python is YcmCompleter GoTo, which usually sends you to where the function/class was defined in code. That is the line just above the docstring, which lists everything you asked for. I am not sure if the inline commenting is in the same place in C/Java/C++. – user1717828 Mar 19 '19 at 17:58
10

For C, pressing K on the keyword will pull up the built-in manpage directly. For instance, place the cursor on the printf keyword:

printf("Hello, %s!", foo);

Now press K (upper case K) and the manpage for printf should appear in VIM:

NAME
   printf - format and print data

SYNOPSIS
   printf FORMAT [ARGUMENT]...
   printf OPTION

DESCRIPTION
   Print ARGUMENT(s) according to FORMAT, or execute according to OPTION:

   --help display this help and exit

   --version
          output version information and exit

   FORMAT controls the output as in C printf.  Interpreted sequences are:

   \"     double quote
   \\     backslash

   ... It continues for quite a few scrollable pages

This seems to work out of the box in all Debian-derived distros that I've tried it on, no specific configuration required.

| improve this answer | |
  • 1
    That's the manpage of printf(1) the command, not printf(3) the function. In any case, the C function manpages are from manpages-dev, which is usually installed by default, but not always, and POSIX functions are in manpages-posix-dev - usually not installed by default, neither is C++ documentation (libstdc++-X.Y-doc). – muru Jan 24 '16 at 9:24
  • 2
    See @PiCTo's excellent new answer for details on how to get this feature to open the desired man page. – Rich Jul 16 at 13:53
5

If you want proper documentation, the plugin in saginaw's comment seems to cover your C++ needs. I don't know of a generic one but you can probably find alternatives for other languages on vim.org.


If…

  • your code is already indexed with ctags,
  • your index also includes the standard library,
  • and all you want is a hint,

you can use <C-w>} to open the definition of the keyword under your cursor in a preview window. YMMV, of course.

See :help preview-window, :help tags and :help ctags.


You can also use Vim's built-in "include-search" functionality: press [i to print the "signature" of the word under the cursor in the command-line:

[i

See :help include-search.

| improve this answer | |
  • 1
    [i is weird. It works for stdlib.h and qsort, but not cstdlib and std::qsort or just qsort. – muru Jan 23 '16 at 20:25
  • For those unfamiliar with the nomenclature as I am/was <C-w> means CTRL-W Which is the nomenclature used in the :help files. You may then use :pc to close the preview window. – danielson317 Nov 21 '19 at 16:49
2

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:

  • modify 'keywordprg' to be man [-s] 3:
    set keywordprg+=\ 3
    
  • remap K to be 3K in modes where K calls 'kp':
    nnoremap K 3K
    vnoremap K 3K
    

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'.

| improve this answer | |
1

Vim's C/C++ file type comes with a decent omnicompletion function, which supports "preview" functionality. So add "preview" to your completeopt option and see a function signature when you do insert mode completion.

In normal mode, you could CTRL+W } to open the function declaration in the preview window manually.

| improve this answer | |
1

As mentioned pressing K on the keyword works by searching a man page. For getting C++ man pages you may try https://github.com/aitjcize/cppman (it gets "man pages" from cplusplus.com and cppreference.com).

| improve this answer | |
0

Install YouCompleteMe. When you place the cursor over an identifier, enter YcmCompleter GetDoc command to get the doc in a little preview window at the bottom. It works across many languages like python and c/c++.

Youcompleteme also displays the preview window during its autocomplete feature. You can customize the behavior of the doc preview with g:ycm_autoclose_preview_window_after_completion variable.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.