16

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
8

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.

  • 2
    "t's a bit annoying to install,"... That's an understatement. Still, it is very good. ^_~ – Sardathrion - Reinstate Monica 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 at 17:58
8

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.

  • 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
4

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.

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

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

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.

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.