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.
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityIs 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.
TL;DR: Set 'path'
to include the path to your include files. In your example:
set path+=/path
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.
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.
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.
Ctrl-X
and Ctrl-I
keys and staying for pattern scanning...
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.
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...