1

Is there a plugin which autocompletes C/C++ code from specific headers given in a specific path (i.e. /path/)?

If I include some headers from that path, I would get autocompletion from given header.

2
  • 1
    What do you mean by "specific headers" ? C++ headers are added for the code to be valid so what is the link with the completion ? Jul 16, 2019 at 7:03
  • i mean, if i have my own created headers with math functions and anything else... so i would love that vim know about my functions for autocompletion... logically, i'm implementing alternative compiler with objs automatically linked... :)
    – MindLerp
    Jul 16, 2019 at 15:54

1 Answer 1

3

TL;DR: Set 'path' to include the path to your include files. In your example:

set path+=/path

Native completion from include files

Vim native completion is quite useful for C and C++ files, Vim can search for your include files and find candidate completion targets in them and the defaults in Vim are already pretty useful for Vim completion.

You can use the CTRL-X CTRL-I sequence to initiate keyword completion, completing keywords in the current and include files.

And CTRL-X CTRL-D to initiate define completion, completing macros and defines in the current and include files.

set path

The important option to customize for these is 'path', which tells Vim where to search for include files.

By default, Vim will search the directory where the file lives (if you're editing src/submodule/xyz.c, then it will search under src/submodule first), the system include directory (/usr/include) and the current directory (which might typically be the root of your project.)

You might want to add paths to this setting, basically any include path you're passing the compiler through a -I flag should be of interest here.

Other relevant settings

Also relevant are 'include' and 'define', which hold patterns (regexps) used to detect #include and #define directives used for the aforementioned completion commands.

Also 'includeexpr', which can be used to translate an include to a file name (useful for languages in which submodule paths are separated by . rather than /.)

These are already correctly set for C/C++ by default, so you typically don't need to set these unless you're working with a different language.

Other useful commands for include files

There are other commands that can become very useful once 'path' is set correctly to find your include files.

For instance, see include file search for commands such as [i and [d which will show you the first occurrence of a keyword (or a define) and will go inside include files whenever it sees an #include directive. This can be quite powerful, as a quick way to check a function prototype wherever it's declared.

The gf command can be used from an #include line to open that header file. The :find command can be used to open an include file (or really any file under 'path') by name.

4
  • it's a good solution, pretty slow for directories with thousand of includes, but i was looking for something more faster and without the need of press Ctrl-X and Ctrl-I keys and staying for pattern scanning...
    – MindLerp
    Jul 16, 2019 at 20:45
  • i mean, isn't hard to write a real time header scanning, just add some regex stuff checks in a vector with all functions scanned in memory and voilà, but just i was looking to know if already exists one....
    – MindLerp
    Jul 16, 2019 at 21:03
  • @nikomaster I guess the next step is using tags files that you can generate using ctags (Ctrl-X Ctrl-] default keybinding for completion using tags.) Personally I use the vim-gutentags plug-in to keep the tags file up to date. Next would be something like YouCompleteMe or some other code-aware completion plug-in. And, finally, the LSP ones (Language Server Protocol), where you have a daemon continuously compiling your code in background. There are some plug-ins for Vim/NeoVim that give you access to those. YMMV on whichever solution is right for you.
    – filbranden
    Jul 16, 2019 at 21:07
  • 1
    isn't even necessary to setup a local server language management, i found completor.vim, it works pretty well, doesn't use too much resources, optionally you can setup a lsp, but it uses by default the path system's var... also it works asynchronous...
    – MindLerp
    Jul 20, 2019 at 19:02

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.