How do I use keyword completion that searches included files and headers in Vim?
Answer
<C-x><C-i>
Explanation
Vim's <C-x><C-i> completion mode searches for keyword matches not just in the current buffer, but also in all files reachable via include directives (e.g., #include in C/C++, import in Python). This lets you complete function names, types, and constants defined in headers without leaving insert mode.
How it works
<C-x>enters Vim's sub-completion mode from insert mode<C-i>selects "include-file completion" — searches the current file and all files it includes, recursively- The
includeoption tells Vim how to recognize include statements (default handles C-style#include "..."and#include <...>) - The
pathoption is used to resolve include file locations - Once the menu appears, use
<C-n>/<C-p>to navigate,<CR>or<C-y>to accept
Example
In a C file:
#include <stdio.h>
With the cursor in insert mode after typing pri, pressing <C-x><C-i> opens a completion menu with printf, pread, pwrite, and other symbols from stdio.h and its transitive includes.
Tips
- Run
:set include?to see the current include pattern; customize it for your language with:set include=... - Run
:set path?to verify Vim can find your header directories; add paths with:set path+=/usr/local/include - To make
<C-n>also search includes by default, addito thecompleteoption::set complete+=i - For just the current file (no includes), use
<C-x><C-n>instead - Similar completions:
<C-x><C-]>for ctag-based completion,<C-x><C-k>for dictionary words