30

Some editors (such as visual studio on windows) do C++ autocompletion which understand C++. For example, given:

#include <vector>

int main(void) {
  std::vector<int> v;
  v.i

In visual studio the auto-completion knows the only method on std::vector<int> that starts with an i is insert.

Is it possible to get this kind of autocompletion in vim?

  • 3
    The YouCompleteMe plugin can learn terms to autocomplete, and can understand C family languages. – alxndr Feb 3 '15 at 18:16
  • @alxndr Documentation was pretty confusing for me-in case for installation with vundle. And as soon as I started out with it , YCM server crashed! – Abhinav Gauniyal Feb 3 '15 at 18:19
  • YCM works for me relatively well, unless I have template heavy (boost.geo) stuff where it adds looooots of laod to the system. My setup is a mess meanwhile, hard to extract YCM parts ... – johannes Feb 3 '15 at 18:43
  • @yo' This question is fairly specific to using vim with C++. Solutions for it probably won't apply to someone who uses Vim to write Perl (and vice versa). So c++ seems fine to me; but of course Vi and Vim Meta is the right place to discuss that. – derobert Feb 3 '15 at 19:17
  • 2
    @AbhinavGauniyal That sounds similar to problems I've seen before with using YCM. What system are you on? For OSX see here. I'm not using MacVim, but I installed CMake with Homebrew. Then I cded to the YCM plugin directory (~/.vim/bundle for me), and ran ./install --clang-completer. – bsmith89 Feb 3 '15 at 22:37
23

I really like clang_complete for this. It does require clang, and you need to tell it where libclang resides in your system. After that, it works wonderfully.

People might suggest YouCompleteMe, but to be honest, that plugin is hugely bloated for what it says it does, and it requires way too many steps to install. I also had it segfault Vim on multiple occasions. I couldn't be happier with clang_complete.

This is what I get when I type v.: clang-complete

  • 4
    Agreed. I've had lots of problems with YCM (including the mentioned segfaults). And even after hours of configuration, I never had it fully working. – CharlesL Feb 5 '15 at 12:45
  • 1
    I had YCM server crashed numerous times , looking forward to clang_complete , here is relevant information for other users : github.com/Rip-Rip/clang_complete/blob/master/doc/… – Abhinav Gauniyal Feb 5 '15 at 16:12
  • 1
    I had the reverse. YCM worked fine (even if it was a PITA to install) but clang-complete never worked... – Sardathrion Mar 16 '15 at 9:12
  • Can you hide all of that so you can see your code a few lines down? – Matt C Apr 29 '16 at 2:18
  • @MatthewCliatt, hide what? The popup menu? There's nothing special in the code. It's just return 0 underneath that popup menu. – akshay Apr 29 '16 at 7:28
8

YouCompleteMe (Link) plugin has been work great for me. It uses libclang to generate the autocomplete feature, providing accurate completion.

It has a lot of customization, specially when working with compilation flags. You can edit the "flag generator" editing a python script per project (Example). But, to me the main advantage is that it supports Clang Compilation Database (Link!). This means that you can compile your code normally and ask clang to spit all the flags its used for the compilation, and use those flags with YouCompleteMe. Very handy if you want your autocompletion tool to be aware of your macro definitions or the location of all the headers files in your system without any effort of typing it manually.

3

YouCompleteMe isn't that hard to install, and it has been quite stable for me, I've migrated from clang_complete to YouCompleteMe years ago. One thing I agree, it's indeed bloated with a huge commit history, many internal dependencies and support for many languages you may not care for but still will end up cloning its associated submodules. That, that's an issue with YouComplemeMe, still.... it does the work.

I've worked on a fork with special purpose to better support C++, you may check this gif showing it off:

https://s3.amazonaws.com/f.cl.ly/items/1e2F0A123h331c1G0L0R/SadBart.gif

It leverages new parameter information that Clang is able to provide, you may learn more about this from this article of mine: http://nosubstance.me/articles/2015-01-29-better-completion-for-cpp/.

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.