Diwas
Diwas

Reputation: 167

Is there a way to get help for some C functions inside of vim/Neovim?

This question may be a little off topic. But I was wondering if there was a way for me to look at the descriptions of C functions using vim or neovim. Is it possible to look at their documentations by doing something like :help? This would really be helpful since I wouldn't need to lookup to my browser everytime.

I am unclear about these things:

Upvotes: 1

Views: 1733

Answers (4)

r_31415
r_31415

Reputation: 8982

The previous answer recommending cppman is the way to go. There is no need to install a bulky language server just for the purpose of having the hover functionality. However, make sure you're caching the man pages via cppman -c. Otherwise, there will be a noticeable delay since cppman is fetching the page from cppreference.com on the fly.

If you like popups for displaying documentation, convert the uncompressed man pages (groff -t -e -mandoc -Tascii <man-page> | col -bx), and set keywordprg to your own wrapper to search for keywords according to your needs.

enter image description here

Upvotes: 1

Matt
Matt

Reputation: 15186

Let's explain how "K" command works in more detail.

  • You can run external commands by prefixing them with :! command. So running man tool is as easy as
:!man <C-R><C-W>

Here <C-R><C-W> is a special key combination used to put word under cursor from text buffer down to command line.

Same for showing Vim's built-in help page

:help <C-R><C-W>
  • As it feels tedious to type that, Vim also defines K Normal mode command that does pretty much the same thing. Except the tool name is taken from value of an option named "keywordprg".

So doing set keywordprg=man (default for *nix systems) makes K to invoke !man tool; while set keywordprg=:help is for bultin help.

Also, the option :h 'keywordprg' is made global or local-to-buffer, so any Vim buffer is able to overwrite global setting. For example, this is already done by standard runtime for "vim" and "help" buffers, so they call ":help" instead of "man".

  • The problem with :!man command is that it shows "black console". It'd be nice if we could capture man's output and open it inside Vim just like a builtin help page. Then we could also apply some pretty highlighting, assign key macros and all such. This is a pretty common trick and it is already done by a standard plugin shipped with Vim/Neovim.

A command that the plugin provides is called :Man, so you can open :Man man instead of :!man man, for example. The plugin is preactivated in Neovim; for Vim you still need to source one file manually. So to make use of this plugin you'll need something like this

set keywordprg=:Man
if !has("nvim")
    source $VIMRUNTIME/ftplugin/man.vim
endif

Upvotes: 0

DevSolar
DevSolar

Reputation: 70333

By pressing K, the keyword under the cursor is looked up using a configured keyword lookup program, the default being man. This works pretty much out of the box for the C standard library.

For C++, you might want to look at something like cppman.

Upvotes: 5

TornaxO7
TornaxO7

Reputation: 1299

Well yes, you can get the description of C functions by using a LSP (language server plugin)! Here is an image of me using clangd as my LSP: information example

You'd "just" need to install the LSP and start it. I don't know how familiar you're with neovim, but just in case if you don't know how to install a plugin or to be more specifique: If you don't know how you can install a LSP server, then you can do the following:

  1. There're plenty videos how to set up a LSP-Server for your language. Here's an example.
  2. If you don't want to set up on your own, you can pick up one of the preconfigured neovim setups (some of my friends are recommending lunarvim)

But yeah, that's it. If you have any further questions feel free to ask them in the comments.

Happy vimming c(^-^)c

Upvotes: 1

Related Questions