30

I code c++, using vim.

Often times, I find myself wasting time (and brekaing flow) looking up trivial things like:

is std::string.substring does it take (start, length) or (start, end).

This often results in open browser; wait; search on google; first link useless, try second link; okay, done.

How do others do this in vim? Is there a nice *.tgz I can download of standard function documentation and somehow reference them inside of vim?

Thanks!

DevSolar
  • 67,862
  • 21
  • 134
  • 209
anon
  • 41,035
  • 53
  • 197
  • 293
  • While I appreciate this Q (and have installed cppman myself as a result), the obvious first improvement would be to point your browser to cppreference.com and use that instead of a generic Google search. (After all, cppman doesn't do much else...) – DevSolar Mar 05 '22 at 15:54

6 Answers6

31

You can also use cppman which provides:

C++ 98/11/14 manual pages for Linux/MacOS

enter image description here

Then you could use it in place of man when typing ShiftK in Vim (cf. @alesplin's answer):

autocmd FileType cpp set keywordprg=cppman

That would open a nice man-like page with the STL documentation that you can navigate with the Vim pager.

For a better integration with Vim, it could probably be used with vim-man or any other similar plugin.

Personally, I bypassed keywordprg by remapping ShiftK for C++ files, and I open a tmux split:

command! -nargs=+ Cppman silent! call system("tmux split-window cppman " . expand(<q-args>))
autocmd FileType cpp nnoremap <silent><buffer> K <Esc>:Cppman <cword><CR>

tmux

BenC
  • 8,729
  • 3
  • 49
  • 68
  • :call system("tmux split-window cppman ") does not do anything. -> the mapping does not work in my setup... do you know why? – Jan Nov 11 '16 at 11:02
  • @Jan: do you have tmux installed? – BenC Nov 12 '16 at 15:07
  • yes. The command is also working inside a normal terminal... So `tmux split-window cppman vector` executed in a terminal, splits a new tmux window open with the cppman page of vector – Jan Nov 13 '16 at 09:47
  • Here is a small plugin that makes it easier to use cppman from within vim: https://github.com/gauteh/vim-cppman – gauteh Aug 10 '18 at 10:17
11

I don't program in C++, but if there are man pages for the functions in question, you can access them by placing the cursor over the function name and hitting ShiftK. This only works for functions that have a man page installed, so your mileage may vary.

alesplin
  • 1,332
  • 14
  • 23
5

Recommend zeal, it's an offline documentation browser. With zealvim, you can just use \z to get the definition of current word base on filetype.

Hongbo Liu
  • 2,818
  • 1
  • 24
  • 18
4

This might help:

OmniCppComplete - C/C++ omni-completion with ctags database

Also take a look at this:

C++ code completion

You can also take a look at Vim Intellisense for C++:

Vim Intellisense - C++ Plug-in

Robert S. Barnes
  • 39,711
  • 30
  • 131
  • 179
1

I know it's an old question, but I was searching for the same thing and none of the answer was helpful for me. If anyone else come to this question, they might find this useful.

You should use CRefVim Plugin. With this you will have full access to c reference manual. It's for C, not for C++, but still it's very handy.

The usage:


There are several ways to specify a word CRefVim should search for in order 
to view help: 

cr normal mode: get help for word under cursor Memory aid cr: (c)-(r)eference cr visual mode: get help for visually selected text Memory aid cr: (c)-(r)eference cw: prompt for word CRefVim should search for Memory aid cw: (c)-reference (w)hat cc: jump to table of contents of the C-reference manual Memory aid cc: (c)-reference (c)ontents

Note: by default is \, e.g. press \cr to invoke C-reference

Note: The best way to search for an operator (++, --, %, ...) is to visually select it and press cr.

To get help do :help crefvimdoc To show the C-reference manual do :help crefvim

You can use :help too. To search help for a function use it like this

:help crv-FUNCTION_NAME

With :h crv-strlen, you will find help for strlen() function. :h crv-operator will help you to find the section about C operators.

sakibmoon
  • 2,026
  • 3
  • 22
  • 32
0

Not really an answer as it is not "inside vim"... but why don't you cut the "open browser, search Google" part? Just point your browser at a good API documentation, and keep it open. http://www.dinkumware.com/manuals/default.aspx is my favourite (which can be downloaded for offline reference, too), http://www.cplusplus.com/reference/ is not bad either. A window switch and two or three clicks later you have your answer; I doubt an "inside vim" solution could be much less disruptive to your workflow.

That being said, having a solution inside vim would be nice for those who are working on a text-only interface, so I am looking forward to the other answers. ;-)

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    This counts as wasting time and breaking flow: switching to another app and searching means having to use the mouse, move around windows, etc. Not what the question asks for, and not what I'd want if given a choice. – Seth Johnson Feb 25 '10 at 21:09
  • I don't see a significant difference between Shift-K for a manpage (@alesplin), (@R.S.Barnes), and Alt-Tab - / - . You might also note that the other two solutions only help you if you have the search string in your code already, while you can look up anything using my method, which I use for many years now. I admitted it is not exactly what the OP asked for, but the way both he and yourself made browser search look more difficult than necessary makes me believe you can't handle more than one app at a time. Strong words, but so is "wasting time and breaking flow". – DevSolar Feb 26 '10 at 06:03
  • Would w3m help? Cppman worked like a charm for me, but browsing in general will still break your workflow. w3m (and w3m-img) bring browsing to the terminal, and a multiplexer like tmux means you don't even have to close vim. – John P Aug 17 '15 at 17:37