48

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?

6
  • 3
    The YouCompleteMe plugin can learn terms to autocomplete, and can understand C family languages.
    – alxndr
    Feb 3, 2015 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! Feb 3, 2015 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, 2015 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, 2015 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, 2015 at 22:37

3 Answers 3

39

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

5
  • 6
    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, 2015 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/… Feb 5, 2015 at 16:12
  • 1
    I had the reverse. YCM worked fine (even if it was a PITA to install) but clang-complete never worked... Mar 16, 2015 at 9:12
  • Can you hide all of that so you can see your code a few lines down?
    – Matt C
    Apr 29, 2016 at 2:18
  • 1
    @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, 2016 at 7:28
9

One option (what I currently use)

I don't use YouCompleteMe anymore. coc-clangd is much better and simpler.

Today I use coc.nvim + coc-clangd instead.

Another option (what I used to use)

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


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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