How do I autocomplete macro-defined identifiers from header files while in insert mode?
Answer
<C-x><C-d>
Explanation
<C-x><C-d> in insert mode triggers defined identifier completion — it searches for identifiers that match the partial word before the cursor by scanning #define statements in the current file and any files reachable through #include directives. This is distinct from generic keyword completion (<C-n>) because it targets only preprocessor-defined symbols.
How it works
- Pressing
<C-x><C-d>opens a completion menu populated by#defineidentifiers - Vim uses the
defineoption pattern to identify what counts as a definition (default:^\s*#\s*define) - It searches the current file and all files scanned via
#include(controlled by theincludeoption) - Navigate the popup with
<C-n>/<C-p>to select the completion - Closes automatically when you select or dismiss
Example
Given the header limits.h contains:
#define MAX_BUFFER_SIZE 4096
#define MAX_CONNECTIONS 100
In your source file, typing MAX_B and pressing <C-x><C-d> offers MAX_BUFFER_SIZE as a completion.
Tips
- Unlike
<C-x><C-i>(keyword completion from includes),<C-x><C-d>is scoped to only#definemacros - Useful in C/C++ to complete constants, feature flags, and macro names without memorizing exact spelling
- Works without ctags or LSP — it uses Vim's built-in include scanning
- Customize which files to scan:
:set include=and:set path+=... - Reference:
:help i_CTRL-X_CTRL-D